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 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOM: How to get a node element from an object id?

    Hello,

    I want to make a call like this (instead of document.getElementsByName("ANOTHERID")):

    document.getElementById("MYID").getElementsByName("ANOTHERID")

    Why can't I make this function call?

    When accessing the object like the example below, there is no problem:

    myBody=myDocumentElements.item(0);
    myBodyElements=myBody.getElementsByTagName("p");

    How can I get the element from the object I retrieve by using the getElementById function?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    getElementsByName indexes objects by their NAME attribute, not their ID - an ID must be unique, but a NAME can be used for multiple elements, hence getElementsByName forms a collection where getElementById is a single object.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well that's not the problem !

    The problem is that I can't make a tag search for specified HTML block using getElementById function.

    Maybe I should have written instead document.getElementsByName("ANAME")) of document.getElementsByName("ANOTHERID"))

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe I should have written document.getElementsByName("ANAME")) instead of document.getElementsByName("ANOTHERID"))

  • #5
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Essex, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry if I've got the wrong end of the stick, but do you mean you want to select the element on the basis of its tag name? If so, then you want getElementsByTagName instead

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not exactly...

    I want to get an element by its id (not its tag name) and then perform a getElementsByName on it.

    For example if I have this HTML code:

    <td>
    ... some HTML code ...
    </td>
    <td id="myid">
    <div name="aname">...</div>
    <div name="aname">...</div>
    <div name="aname">...</div>
    </td>
    <td>
    ... some HTML code ...
    </td>

    Instead of using document.getElementsByName("aname") which is traversing the whole HTML document, I'd like to perform a getElementsByName only in the HTML block contained in the column with id="myid"...

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    getElementsByName is a method to only the document object.

    You could prototype your own in Gecko (et KHTML?), but IE doesn't support the HTMLDom, so you'd need to make a DHTML behavior to get a similar result.

    Or, you could just live with it how it is.
    Last edited by beetle; 06-19-2003 at 05:02 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    While getElementsByTagName is part of the core DOM specification:
    http://www.w3.org/TR/2000/REC-DOM-Le...l#ID-745549614
    getElementsByName is defined only for the HTMLDocument object.
    http://www.w3.org/TR/2003/REC-DOM-Le...ml#ID-26809268

    Considering your particular example:
    1. Starting HTML 4.01 (maybe earlier) name attribute can not be used with div element (and most elements as well). It was left mostly for backward compatibility with form elements. So you should not be using it like that.
    2. You can retrieve the same list of nodes by:
    document.getElementsById('myid').getElementsByTagName('div');
    or if you remove all the extra white space within the myid element by simple:
    document.getElementsById('myid').childNodes

    EDIT beetle edited his replay behind by back
    Last edited by Vladdy; 06-19-2003 at 05:18 PM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #9
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thanks, that was clear!

  • #10
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I'm moving this to the DOM subcategory.


  •  

    Posting Permissions

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