Reputation: 87
hi can anyone tell me what should i write so that on every iteration of for loop option's value changes and i get one by one option's value. following is the code:
for (var i=1; i<4; i++)
{
$("#mydropdown_for_month option[value=i]").attr('hidden','hidden');
}
for example dropdown has 10 values but i want to hide those options which have the values dynamically changed by for loop so i write " option=i " but it is not working. I mean i want to write variable i. how can i do that
Upvotes: 0
Views: 243
Reputation: 26591
You need to call i
as a variable:
[EDIT 2] Thanks to ThiefMaster, here is a nicer code :
for (var i=1; i<4; i++) {
$("#mydropdown_for_month option[value=" + i + "]").prop('hidden', true);
}
[EDIT] Without hidden attribute (which exists only in HTML5) - thanks to Phil and ThiefMaster:
for (var i=1; i<4; i++) {
$("#mydropdown_for_month option[value=" + i + "]").hide();
}
Upvotes: 2
Reputation: 31033
for (var i=1; i<3; i++)
{
$("#mydropdown_for_month").find('option[value="'+i+'"]').hide();
}
here is the demo http://jsfiddle.net/DBXGe/
Upvotes: 0
Reputation: 262969
You don't have to use a loop here (actually, loops are quite rare when you're using jQuery in the way it was designed for).
You can use the slice() method to match a subset of the <option>
elements and hide them all at the same time:
$("#mydropdown_for_month option").slice(1, 4).hide();
Upvotes: 1
Reputation: 1846
Try
"option=" + i
So that the 'i' is a variable rather than a string
Upvotes: 1
Reputation: 11175
You can try, $("#mydropdown_for_month option").eq(i).hide()
didn't test it, just a guess :D
Upvotes: 1