James
James

Reputation: 87

How to programatically select an option from MaterializeCSS's select?

I initialized the select with materialize_select() but I want to switch the currently selected option via code. Re-ordering and re-initializing the list both seems silly and would ruin the order of the list (which I would like to preserve).

For example I have a select with options: A, B, C

A is currently selected but I want to select B.

How can I accomplish this?

Upvotes: 1

Views: 1765

Answers (1)

MBahamondes
MBahamondes

Reputation: 360

First thing you do is, in my case through Jquery , select the value that you want and then select Initialize. Look:

$(document).ready(function(){
    $("select[id$='idSelect'] option[value=1]").attr("selected", true);
    $('select').material_select();
});

I leave an example: Example Select

Upvotes: 4

Related Questions