Mercury - How do I set an option element's 'selected' property?

119 views Asked by At

How do I set the selected property of an option element with Mercury/virtual-dom?

I've tried the following, but no selected property appears on the <option>:

h('select', {
  name: 'selectedRole', 
}, [
  h('option', {value: 'user', selected: true}, 'User'),
])

Instead, it results in this:

<select name="selectedRole">
  <option value="user">User</option>
</select>
2

There are 2 answers

0
aknuds1 On BEST ANSWER

After some testing, it appears this is how it works with Mercury. When you set selected to true on an option, it does become selected even though the property isn't visible on the HTML element.

However, I can only make it work in a small demo, not in my full application. Thus, there could be a bug in virtual-dom.

0
Casey Caruso On
    $("option[value='user']").attr('selected', true);

I have also had this problem with the html5 datalist tag's list attribute. One work-around is to use javascript, or jquery, to dynamically assign this attribute.