Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<select> with no options matching the bound value should not select the first item #2494

Closed
MartinKolarik opened this issue Apr 5, 2016 · 3 comments

Comments

@MartinKolarik
Copy link
Member

Description of the bug:
When a bound <select> value is set to something that doesn't match any options, it is ignored, and first <option> is selected instead.

Related: http://stackoverflow.com/questions/36409229/using-ractive-js-how-do-you-unselect-a-selected-select-box

Versions affected:
both 0.7.3 (<select> and model go out of sync) and edge (model value replaced by first <option>)

Reproduction:
http://jsfiddle.net/7bx6pc8h/1/

Code:

{{selected}}<br>

<select value="{{selected}}">
  <option>1</option>
  <option>2</option>
</select>
var ractive = new Ractive({
  el: 'body',
  template: '#template',
  data: { selected: 'x' }
});

setTimeout(() => {
  ractive.set('selected', 'y');
  console.log(ractive.get('selected')); // logs 1
}, 3000);

Note:
The current behavior is correct in a situation where there had been a selected <option>, but it was removed from the DOM. In that case, we should select the first option. However, I think this should not be the case when ractive.set() is called explicitly.

@evs-chris
Copy link
Contributor

I think this is more or less forced by browsers now. Firefox as of mid-last-year (at least that's the first encounter I had with it) and Chrome as of 49 don't seem to let you have a select with options and no selected value. There's a strong chance I've missed something though 😄

@martypdx
Copy link
Contributor

martypdx commented Apr 5, 2016

Try select.selectedIndex = -1

@MartinKolarik
Copy link
Member Author

@evs-chris I posted this as a solution on SO and it works in all browsers.

evs-chris added a commit that referenced this issue Apr 5, 2016
evs-chris added a commit that referenced this issue Apr 6, 2016
Don't force the first option to be selected in a select with no matching value - #2494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants