Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 02-08-2009, 09:43 AM childNodes confusion
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
I have the following script to check for the first child element of each div with the class 'tabSection'.

Code:
var tabContain = document.getElementById ('tabContain');
var tabBox = document.getElementById ('tabBox');
        
// determine amount of tabs
// get number of sections
var j = 0;
var section = new Array ();
for (i = 0; i < tabBox.childNodes.length; i ++)
    {
        if (tabBox.childNodes[i].className == 'tabSection')
            {
                section[j] = tabBox.childNodes[i];
                j ++;
            }
    }

alert (section[0].childNodes.length);
for (i = 0; i < section.length; i ++)
    {
        alert (section[i].firstChild.nodeName);
    }
Code:
    <h1>Tab Generator
    </h1>
    <div id="tabContain">
      <div id="tabBox" style="border:#444 solid 1px">
        <div class="tabSection">
          <h2>div one</h2>
          <p>Content for New Div Tag Goes Here</p>
        </div>
        <div class="tabSection">
          <h2>div two</h2>
          <p>Content for New Div Tag Goes Here</p>
        </div>
        <div class="tabSection">
          <h2>div three</h2>
          <p>Content for New Div Tag Goes Here</p>
        </div>
      </div>
    </div>
<script type="text/javascript" src="tab.js"></script>
<script type="text/javascript">
tabBoxHeight ('200px');
</script>
However the alerts at the end show that each div appears to have 5 child elements (when I expected 4). It also states that the nodeName of each firstChild is '#text', when I would expect it to be a heading tag.

Does anyone know what code I should be using to access the heading of each div? At the moment I'm using 'section[i].firstChild' but with no success!
__________________

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 02-08-2009 at 09:44 AM..
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 02-08-2009, 10:38 AM Re: childNodes confusion
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
It comes down to this:
Because you have your HTML formatted neatly, there is some whitespace between the <div> and <h2>, which accounts for the #text you are finding.

Just loop the childNodes again and for each, just check the tagName.

Code:
for (i = 0; i < section.length; i ++)
    {
        for(name in section[i].childNodes){
              if(section[i].childNodes[name].tagName == 'H2'){
                      // do your stuffs here
                      // but don't forget to break :P
                      break;
              }
        }
    }
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 02-08-2009, 02:22 PM Re: childNodes confusion
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks! That's solved a lot of confusion
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to childNodes confusion
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11933 seconds with 12 queries