Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change value of materialize select box by jquery

I want to change materialize select box value by jquery.

I am using $('#myselect').val('1'); on onchange event of other select box but it not works.

$("#select1").change(function() {
    $('#myselect').val('1');
});
like image 715
phpboy Avatar asked May 20 '15 05:05

phpboy


3 Answers

It appears to work fine for me, changing the first drop down, resets the value of the second drop down to 1.

I have done a rough implementation on jsFiddle: http://jsfiddle.net/55r8fgxy/1/

<select id="select1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

<select id="myselect">    
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

JS:

$(function() {
    $("#select1").on('change', function() {
        $('#myselect').val("1");

        // re-initialize material-select
        $('#myselect').material_select();
    });
});
like image 170
logikal Avatar answered Oct 23 '22 14:10

logikal


$('#myselect').formSelect() ;

The new method is formSelect(), use this after you have updated the select.

like image 31
Ankur Shukla Avatar answered Oct 23 '22 12:10

Ankur Shukla


As suggested by @logikal, you have got to re-Initialize

$("#myselect").material_select()
like image 12
Ashish Ranjan Avatar answered Oct 23 '22 13:10

Ashish Ranjan