How to disable a specific item in a dropdown item

How to disable some elements of a dropdown element using jQuery or JavaScript?

+4
source share
5 answers

Similarly, you can disable any other HTML element, use

$(/* option selector */).prop('disabled', true); 

See the action .

+6
source

Easy!

 <select> <option value="Opt 1.">Opt 1.</option> <option class="optionselector" value="Opt 2. I'm disabled!" disabled="disabled">opt 2. I'm disabled!</option> </select> 

Just add disabled="disabled" to the tag.

To do this in jQuery, make sure you have the latest version installed, and then use javascript.attr () to add the disabled="disabled" attribute as needed:

 .click(function(){ $('.optionselector').attr("disabled","disabled"); }); 

Of course, you will need to install .click inside another event or function, so .click is triggered by SOMETHING, in particular, this can be used to say "When I .click () this button, then add .attr ()", etc. .

+1
source
 $(document).ready(function(){ $('#id').attr('disabled','disabled'); }) 

and html

 <form id="fmname" method="get"> <select > <option id="id">s</option> </select> <input type="submit" /> </form> 
0
source
 $("option").attr("disabled", "disabled"); 

Just select the options you want using another selector.

0
source

A lot of jQuery, in plain js you get a parameter link, but just set the disabled property to true. So, given:

 <form id="aForm" ...> <select name="aSelect"> <option ...>zero <option ...>one <option ...>two <option ...>three </select> ... </form> 

then disable all options:

 var options = document.forms['aForm']['aSelect'].options; for (var i=0, iLen=options.length; i<iLen; i++) { options[i].disabled = true; } 

Of course, you can disable only one, based on any criteria that you want.

0
source

All Articles