Java-Seekar
Java-Seekar

Reputation: 1770

Disable the Drop down options

I want to disable the drop down options based on the condition. I want to disable all the other options except the option which has the text "Java".

Ex:

<select id="ddlList">
   <option value="csharp">C#</option>
   <option value="vb">VB.NET</option>
   <option value="jquery">jQuery</option>
   <option value="java">Java</option>
</select>

In this case Only java option should be enable and others should be disable.

Upvotes: 1

Views: 2964

Answers (5)

deviloper
deviloper

Reputation: 7240

$("#ddlList option").each(function(){
    if(!$(this).val().contains("java"))
        $(this).attr('disabled', true);
});

This code lets you set the disabled attribute of each dropdown option by checking if its value contains "java"

JSFIDDLE DEMO

Upvotes: 0

Aminul
Aminul

Reputation: 1738

You can write your code like this:

Select Language: <select id="ddlList">
<option value="csharp">C#</option>
<option value="vb">VB.NET</option>
<option value="jquery">jQuery</option>
<option value="java">Java</option>
</select>

JavaScript:

$(document).ready(function(){
    var condition = true;
    if(condition)
    {
        $("select option").prop('disabled',true);
        $("select option[value='java']").removeAttr('disabled');
        $("select").val("java")
    }
    else
    {
        //apply your logic here
    }
});

Demo Fiddle

Upvotes: 0

potashin
potashin

Reputation: 44581

JQuery :

$('#ddlList option:not([value=java])').prop('disabled', true);

JSFiddle

JavaScript :

 var filter = document.querySelectorAll('#ddlList option:not([value=java])')
 Object.keys(filter).forEach( function(g){ filter[g].disabled = true })  

JSFiddle

Upvotes: 1

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Use the disabled property.

$("#ddlList option[value!='java']").prop("disabled", true);

If you want more control, in a more practical way, you can go over each option:

$("#ddlList option").each(function () {
    var allowed = ["java"];
    if (allowed.indexOf($(this).val()) === -1) {
        $(this).prop("disabled", true);
    }
});

Upvotes: 0

Arunprasanth K V
Arunprasanth K V

Reputation: 21931

function optionDisable(selectId, optionIndex){
    document.getElementById(selectId).children[optionIndex].disabled="disabled";
}

or

document.getElementById(optionid).style.display = 'none';

or

function makeDisable(){
    var a=document.getElementById("ddllist")
    a.disabled=true
}

Upvotes: 0

Related Questions