var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
if (document.getElementById("value_[0-9]"))
{
alert('found: ' + inputs[i].value);
}
else
{
alert(inputs[i].value);
}
}
I'm using the condition to get only input IDs that starts with value_
if (document.getElementById("value_[0-9]"))
but nothing is true where I have 5 or more IDs in my form. i.e. value_1, value_2, value_3 etc..
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
if (document.getElementById("value_[0-9]"))
{
alert('found: ' + inputs[i].value);
}
else
{
alert(inputs[i].value);
}
}
I'm using the condition to get only input IDs that starts with value_
if (document.getElementById("value_[0-9]"))
but nothing is true where I have 5 or more IDs in my form. i.e. value_1, value_2, value_3 etc..
Share Improve this question edited Mar 3, 2023 at 22:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 14, 2013 at 13:52 RafRaf 7083 gold badges13 silver badges35 bronze badges3 Answers
Reset to default 5You use wrong method for that. Simply check the id
property of a certain element in the list:
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].id.indexOf("value_") === 0) {
// alert("Found: " + inputs[i].value);
}
}
Combine document.querySelectorAll
with Array.prototype.slice
to make it an Array
Array.prototype.slice.call(document.querySelectorAll('input[id^="value_"]'));
From here you can do your favorite loop, e.g. Array.prototype.forEach
[].map.call(document.getElementsByTagName( 'input' ), function( input ) {
return input.id.indexOf( 'value_' ) === 0;
}).forEach(function( input ) {
// do whatever here
});