ID de elemento por javascript

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.

preguntado el 09 de enero de 11 a las 03:01

2 Respuestas

If there's some reason you don't want to do:

var el = document.getElementById('data'); could iterate over the collection:

var inputs = document.getElementsByTagName("input"), 
    len = inputs.length,

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; en lugar de el = inputs[ len ];

Respondido el 09 de enero de 11 a las 06:01

Puedes usar:


Cause id's are unique.

Respondido el 09 de enero de 11 a las 06:01

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. - Aditya Shukla

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.