Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle disabled property on materialize dropdown selection?

I want to default a select in materialize as disabled.

<form>
 <select id="mySelect" required disabled>
  <option>Cats</option>
  <option>Dogs</option>
 </select>
</form>

<button id="try" >Try it</button>

Jquery:

$('#try').click(function(){
 $('#mySelect').prop("disabled", false);
};

In the dev tools it shows that the disabled property is removed on the select when the try button is clicked. However, the dropdown will not work. I believe this is something specific to materialize because it seems to dynamically generate a styled unordered list above my select. It adds the disabled to this newly generated list, which I removed as well.

like image 208
Spencer West Avatar asked Nov 29 '25 02:11

Spencer West


2 Answers

To get the select to work with Materializecss , you must add the following js line to your script :

$(document).ready(function() {
   $('select').material_select();
});

And to make sure it works after enabling the select edit your js function as follow :

 $('#try').click(function(){
   $('#mySelect').prop("disabled", false);
   $('select').material_select();
});
like image 110
Sam Avatar answered Dec 01 '25 20:12

Sam


To update the select, you need to re-initialize

$('#try').click(function(){
    $('#mySelect').prop("disabled", false);
    var elems = document.querySelectorAll('#mySelect');
    var instances = M.FormSelect.init(elems);
};
like image 33
Hasam Avatar answered Dec 01 '25 21:12

Hasam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!