Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if i have an form elements name, how to get the id?

    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?

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So you're not talking about the ID attribute?

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

    ... And yes, I like Smashing Pumpkins.

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Apr 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    like

    Code:
    <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.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    We usually use the term index about what you spek, not id. But still, I know what you want:
    Code:
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Apr 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much liorean. It works for me even though one has to loop through all of the elements.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •