SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: DOM question

  1. #1
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM question

    Code:
    <span id="crumbs_section"><a href="a.com">Articles</a></span> - <span id="crumbs_category"><a href="b.com">Afghanistan</a></span> // <a href="#">Send to a friend</a>
    using DOM, im trying to grab "Articles" and "Afghanistan". Here are my attempts:

    - document.getElementById('crumbs_section').firstChild.nodeValue; - this gives me "null"
    - document.getElementById('crumbs_section').nextSibling; - this gives me [object text]
    - document.getElementById('crumbs_section').nextSibling.nodeValue; - this gives me "-"

    and i get similar results when trying to grab Afghanistan. I thought that the a-tag is a child of the span tag, and im therefore confused as to why firstChild.nodeValue gives "null"?

    cause of the cms im using, i cant assign an id to the a-tag, only to a tag i wrap it in. can anyone help me out? much appreciated!
    I need someone to protect me from
    all the measures they take in order to protect me

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works:
    Code:
    document.getElementById ('crumbs_section').firstChild.firstChild.nodeValue
    Remember: the first node of the span is the a-element. The first node of the a-element is a text node with node value "Articles"

  3. #3
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, thanks!
    I need someone to protect me from
    all the measures they take in order to protect me

  4. #4
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    
    <script type="text/javascript">
    
    window.onload = function () {
    
        
    alert(document.getElementById('crumbs_section').nextSibling.nodeName); // #text
    
    alert(document.getElementById('crumbs_section').nextSibling.nodeValue); // -
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.nodeName); // span
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.firstChild.nodeName); // A
    
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.firstChild.firstChild.nodeValue ) ; // Afghanistan
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.nextSibling.nodeName ) ; // #text 
    
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.nextSibling.nodeValue ) ; //  //
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.nextSibling.nextSibling.nodeName ) ; // A
    
    alert(document.getElementById('crumbs_section').nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue ) ; // Send to a friend
    
    }
    </script>
    
    
    <span id="crumbs_section"><a href="a.com">Articles</a></span> - <span id="crumbs_category"><a href="b.com">Afghanistan</a></span> // <a href="#">Send to a friend</a>


Bookmarks

Posting Permissions

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