Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - disable selected options

Need to disable already selected options in select box using jQuery. I'd like it to grey out like asmselect.

Test my example here.

//JS $("#theSelect").change(function(){             var value = $("#theSelect option:selected").val();   var theDiv = $(".is" + value);    theDiv.slideDown().removeClass("hidden"); });   $("div a.remove").click(function () {        $(this).parent().slideUp(function() { $(this).addClass("hidden"); });  });  //HTML <body> <div class="selectContainer">     <select id="theSelect">         <option value="">- Select -</option>         <option value="Patient">Patient</option>         <option value="Physician">Physician</option>         <option value="Nurse">Nurse</option>     </select> </div> <div class="hidden isPatient">Patient <a href="#" class="remove" rel="Patient">remove</a></div> <div class="hidden isPhysician">Physician <a href="#" class="remove" rel="Patient">remove</a></div> <div class="hidden isNurse">Nurse <a href="#" class="remove" rel="Patient">remove</a></div> </body>​ 

UPDATED: Here's the finished solution. Thanks to Patrick and Simen.

like image 376
Jeffrey Avatar asked May 19 '10 16:05

Jeffrey


People also ask

How to disable a select option in html?

The disabled attribute is a boolean attribute. When present, it specifies that an option should be disabled. A disabled option is unusable and un-clickable. The disabled attribute can be set to keep a user from selecting the option until some other condition has been met (like selecting a checkbox, etc.).

How do I iterate through select options in jQuery?

Simple jQuery code snippet to loop select box options (drop down boxes) in a form to get the values and text from each option. Useful for manipulating values in form select boxes. $('#select > option'). each(function() { alert($(this).


2 Answers

Add this line to your change event handler

    $("#theSelect option:selected").attr('disabled','disabled')         .siblings().removeAttr('disabled'); 

This will disable the selected option, and enable any previously disabled options.

EDIT:

If you did not want to re-enable the previous ones, just remove this part of the line:

        .siblings().removeAttr('disabled'); 

EDIT:

http://jsfiddle.net/pd5Nk/1/

To re-enable when you click remove, add this to your click handler.

$("#theSelect option[value=" + value + "]").removeAttr('disabled'); 
like image 57
user113716 Avatar answered Oct 07 '22 11:10

user113716


pls try this,

$('#select_id option[value="'+value+'"]').attr("disabled", true); 
like image 32
Siva Anand Avatar answered Oct 07 '22 13:10

Siva Anand