I have a bunch of hidden inputs on a page...
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
Etc...
Each input can have an arbitrary value.
In jquery, what is the best way to check if one of these inputs has been set to a known, specific value?
Thanks
I have a bunch of hidden inputs on a page...
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
Etc...
Each input can have an arbitrary value.
In jquery, what is the best way to check if one of these inputs has been set to a known, specific value?
Thanks
Share Improve this question edited Sep 24, 2015 at 7:26 moffeltje 4,6698 gold badges39 silver badges63 bronze badges asked Feb 22, 2011 at 21:49 travtrav 111 silver badge2 bronze badges3 Answers
Reset to default 4If you know that the value attribute is set to a specific value you can use the attribute selector
$('input[value="something"]');
http://api.jquery./category/selectors/
Edit to add:
you may want to chain the attr selector as input[name="thing"][value="something"]
and @Drackir is right, you can test if it matches by whether one or more elements is in the set via the length
property of the match.
$('input[type="hidden"][value="5084405"]');
hope it helps
The following is what I'd suggest. You want to loop through all of the items with the name and check them and do something for each matching one.
$('input[name="thing"]').each(function() {
var itemValue = this.val();
if(itemValue == "X" || itemValue == "Y")
alert("Item value is " + itemValue);
});