I am using document.getElementsByTagName("input") to get all the input elements in my form.While iterating through them I need to find an element with a particular id say "data" and process it.How can search in the elements such that an element of a particular id exists.
2 Answers
If there's some reason you don't want to do:
var el = document.getElementById('data');
...you could iterate over the collection:
var inputs = document.getElementsByTagName("input"),
len = inputs.length,
el;
while( len-- ) {
if( inputs[ len ].id === 'data' ) { // Test the "id" property.
el = inputs[ len ]; // If a match, grab that one,
break; // and break the loop.
}
}
EDIT: Fixed error where I had el = inputs[ len ].id; instead of el = inputs[ len ];
Comments
You can just use:
document.getElementById("data")
Cause id's are unique.
2 Comments
Aditya Shukla
yes i can do that , but when submitting a form am submitting all the input elements so just have to select that one.
Aditya Shukla
please check the above answer. That's what i wanted.