1

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 2

4

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 ];

Sign up to request clarification or add additional context in comments.

Comments

0

You can just use:

document.getElementById("data")

Cause id's are unique.

2 Comments

yes i can do that , but when submitting a form am submitting all the input elements so just have to select that one.
please check the above answer. That's what i wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.