I want to set the value of an HTML <select> element reactively without changing the various parameters for the element. I found a solution, but it is not elegant.
To test, create a Meteor application with barebone with create meteor select and change the contents of the select.html and select.js files to the following:
select.html
<body> {{> select}} </body> <template name="select"> <label for="select">{{category}}</label> <select id="select"> <option value='' disabled selected style='display:none;'>Select...</option> <option value="Animal">Animal</option> <option value="Vegetable">Vegetable</option> <option value="Mineral">Mineral</option> </select> </template>
select.js
if (Meteor.isClient) { Session.set("category", "") Session.set("label", "Category") Template.select.onRendered(function () { setSelectValue() }) Template.select.helpers({ category: function () { setSelectValue() return Session.get("label") } }); function setSelectValue() { var select = $("#select")[0] if (select) { select.value = Session.get("category") } } }
Now run the application. In the browser console, you can change the value of the category Session variable:
Session.set("category", "Animal")
However, the selection item will not be updated until you change the label:
Session.set("label", "category")
The selection element is now updated, and any subsequent change to the category Session variable will also update the select element.
Session.set("category", "Vegetable")
Is it possible to achieve the same effect directly without using this awkward workaround?