`connect` not picking up initial values of input elements (browser 'back' button)
See original GitHub issueHi there!
We stumbled upon unexpected behavior when using connect
.
Scenario:
- We have a page with radio buttons
- The user selects a radio button
- The user submits the form
- The user uses the
back
button in her browser - Stimulus’
connect
does not recognize the selected radio button
GIF of the behavior:
Stimulus controller
export default class extends Controller {
static targets = ["buttons"]
initialize() {
console.group('initialize')
console.log('target', this.buttonsTarget)
console.log('selected radio button', this.buttonsTarget.querySelector(":scope input:checked"))
console.groupEnd()
}
connect() {
console.group('connect')
console.log('target', this.buttonsTarget)
console.log('selected radio button', this.buttonsTarget.querySelector(":scope input:checked"))
console.groupEnd()
let self = this
setTimeout(function() {
console.group('connect with timeout')
console.log('target', self.buttonsTarget)
console.log('selected radio button', self.buttonsTarget.querySelector(":scope input:checked"))
console.groupEnd()
}, 1)
}
loadEvent() {
console.group('load@window')
console.log('target', this.buttonsTarget)
console.log('selected radio button', this.buttonsTarget.querySelector(":scope input:checked"))
console.groupEnd()
}
}
View
<%= form_tag '/examples' do %>
<div data-controller="radios" data-target="radios.buttons" data-action="load@window->radios#loadEvent">
<div><%= radio_button_tag 'radios', 'a' %> a</div>
<div><%= radio_button_tag 'radios', 'b' %> b</div>
</div>
<%= submit_tag %>
<% end %>
A demo Rails project is available on https://github.com/phylor/stimulus-connect-back-button.
Expected behavior:
The code below should evaluate to the selected radio button. It is null
instead.
connect() {
this.buttonsTarget.querySelector(":scope input:checked")
}
Workaround:
Use setTimeout
within connect
:
let self = this
setTimeout(function() {
self.buttonsTarget.querySelector(":scope input:checked")
}, 1)
Browsers:
- Chrome/qutebrowser: unexpected behavior,
setTimeout
workaround required - Firefox: seems to work as expected, even in
initialize
the selected radio button is found
I have to admit that I managed to get the expected behavior in Chrome as well. But really really seldom. Could be a race condition?
Questions
- Is this just a bug in Chrome?
- Is there something Stimulus could do to remedy the workaround?
- Is there an entire different solution for this problem?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:12 (7 by maintainers)
Top Results From Across the Web
When using the back button, updated values remain in input ...
I am not entirely sure, but I think the values are prefilled by the browser immediately after fully loading the DOM synchronously.
Read more >jQuery messes input values after browser "back button".
I tried to comment off whole script and then all values was all right after back press.
Read more >Forms in HTML documents - W3C
An HTML form is a section of a document containing normal content, markup, special elements called controls (checkboxes, radio buttons, menus, etc.), and...
Read more >How to create an HTML back button - Computer Hope
These buttons don't work if the user has no browsing history. For example, if the user opens your page in a new browser...
Read more >How to stop browser back button using JavaScript
There are so many ways to stop the browser back button most popular and will work in all conditions. You can add code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@leastbad thanks, just used your workaround on my own website. I wonder if the maintainers have enough info now (after almost 2 years) to make this the default behaviour (
#connect
onpageshow
seems legit).I created a library to provide a forms bfcache solution for Turbolinks.
https://github.com/tleish/turbolinks-bfcache-form