hello
hello

Reputation: 351

jQuery disable multiple options by value

I would like to know how to disable multiple options by value. I know I can do this by:

<script>
$('option[value=1],option[value=2]...').prop('disabled', true);
</script>

but I have about 100+ values and there is no particular pattern for the values.

What is the shortest way to do this, if there is?

Upvotes: 0

Views: 1784

Answers (3)

Simcha Khabinsky
Simcha Khabinsky

Reputation: 2039

@martynas has a good answer , but this one is even shorter:

var disabledValuesArr = [5, 4, 3];
$("option[value='" + disabledValuesArr.join("'],[value='") + "']").prop('disabled', true);

JSFiddle

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

A loop may be?

var startingNumber = 1, endingNumber = 100;
for(var i = startingNumber; i < endingNumber +1 ; i++){
   $('option[value="'+ i + '"]').prop('disabled',true); 
}

Or fill an array and loop through it:

var nums = [1,2,3,5,8];
for(var i = 0; i < nums.length; i++){
    $('option[value="'+ i + '"]').prop('disabled',true);    
}

Upvotes: 0

martynas
martynas

Reputation: 12300

var values = [5, 4, 3];

$.each(values, function(k, v) {
     $('option[value=' + v + ']').prop('disabled', true);
});

JSFIDDLE DEMO

Upvotes: 2

Related Questions