0

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?

3
  • Depends on your actual HTML and the values. There are many jQuery selectors (api.jquery.com/category/selectors). You should find one that matches as many elements as possible. Commented Jun 15, 2014 at 6:01
  • Do you have a list of all the values to disable? or is it all of them? Commented Jun 15, 2014 at 6:05
  • @simcha khabinsky I have 100+ random values. Commented Jun 15, 2014 at 6:17

3 Answers 3

2
var values = [5, 4, 3];

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

JSFIDDLE DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

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);    
}

3 Comments

this is true if the values to be selected are 1 to 100 but im trying to select random numbers
@Bernie I answered this when you hadn't edited your question. Your question was unclear and you had 1,2 as your example. What do you think I should've assumed?
That's why I up voted it, I know the question was not entirely clear.
0

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.