When rebinding a material select list with an option selected in the original select, that option is not selected in the generated material select. Instead the previously selected item remains selected.
Select an item, then try typing in the textbox. Inspecting the html shows that the selected item in the original select is updated as per javascript but the material select is not regenerated as expected.
https://codepen.io/anon/pen/bWeywR
Am I doing something wrong or is this a bug?
Is there a method that I've missed to select an element via javascript?
Thanks
Try $('#select').val('default').trigger('change'); or $('#select').val('default');
https://codepen.io/anon/pen/GmqaGW
https://codepen.io/anon/pen/MmedPL
@DanielRuf Both work fine. Thanks for the help!
Any idea why setting the selected attribute didn't work, I was under the impression that's all val() did for a select?
val() should explicitely trigger a change event. Using prop or attr should not do so much in most cases. Also this is much cleaner and was always the recommended way to set an option for a select element ;-)
Excellent thanks, my javascript experience is limited. I've only learnt things as they are needed :)
Thanks for taking to time to let me know.