...

View Full Version : if i have an form elements name, how to get the id?



spybreak
04-10-2003, 10:03 PM
If I know my INPUT's name, lets say: "realName", how can I get it's ID? (What I mean by ID: e.g. the first element in the first Form is document.forms[0].elements[0]...)

Thank you, I appreciate it very much. Does anyone like the Smashing Pumpkins?

Skyzyx
04-10-2003, 10:29 PM
So you're not talking about the ID attribute?

<input name="bob" id="bobby" />

... And yes, I like Smashing Pumpkins.

spybreak
04-11-2003, 07:08 AM
no, I'm not talking about the ID attribute. I'm talking about the element Id.

I need this for the Smashing Pumpkins & Zwan Fan Database (http://www.spfd.org). I'm working on a new form.

spybreak
04-11-2003, 07:23 AM
like




<input type="text" name="email">

<input type="text" name="password">

<input type="text" name="realName">



now, email has the Id [0], password [1] and realName [2]. The thing is, I need to find that out without counting, because I need that to be done automatically as 1) there are many fields and 2) everything will be broken if I hardcode it and add a form input somewhere afterwards.

liorean
04-11-2003, 07:44 AM
We usually use the term index about what you spek, not id. But still, I know what you want:
var
oIndexByNames={},
oElements=document.forms[[string formName | number formIndex]].elements,
i=oElements.length;
while(i-->0)
oIndexByNames[oElements[i].name]=i;I'm afraid you'll have to loop throug the collection and gather the index from it. The way this works, oIndexByNames[[string elementName]] will contain the index of the element with the specified name.

spybreak
04-11-2003, 01:18 PM
Thank you very much liorean. It works for me even though one has to loop through all of the elements.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum