How to select an input element by value using javascript?
with ie6-ie7-ie8
function getInputsByValue(value)
{
var allInputs = document.getElementsByTagName("input");
var results = [];
for(var x=0;x<allInputs.length;x++)
if(allInputs[x].value == value)
results.push(allInputs[x]);
return results;
}
with modern browsers ie9+ (? not sure for ie9 actually) :
document.querySelectorAll("input[value=something]");
You can use document.querySelectorAll()
on modern browsers (https://developer.mozilla.org/En/DOM/Document.querySelectorAll), e.g.
var byValue = document.querySelectorAll('input[value="something"]');
For older browsers you'll have to iterate over the input
s and check the value, e.g.
var inputs = document.getElementsByTagName("input"),
i,
len,
byVal = [],
value = "something";
for (i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].value === value) {
byVal.push(inputs[i]);
}
}