Materialize: Selected Value When Refreshing Select

Created on 23 Apr 2017  路  4Comments  路  Source: Dogfalo/materialize

Description

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.

Repro Steps

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

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

djensen47 picture djensen47  路  3Comments

cope picture cope  路  3Comments

MickaelH974 picture MickaelH974  路  3Comments

artur99 picture artur99  路  3Comments

bradley-varol picture bradley-varol  路  3Comments