question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`connect` not picking up initial values of input elements (browser 'back' button)

See original GitHub issue

Hi there!

We stumbled upon unexpected behavior when using connect.

Scenario:

  1. We have a page with radio buttons
  2. The user selects a radio button
  3. The user submits the form
  4. The user uses the back button in her browser
  5. Stimulus’ connect does not recognize the selected radio button

GIF of the behavior:

screengrab-20200907-1325

image

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:open
  • Created 3 years ago
  • Reactions:8
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
costacommented, May 9, 2022

@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 on pageshow seems legit).

2reactions
tleishcommented, Nov 24, 2020

I created a library to provide a forms bfcache solution for Turbolinks.

https://github.com/tleish/turbolinks-bfcache-form

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found