Posts: 876
Name: Matt Pealing
Location: England, north west
|
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!
Last edited by pealo86; 02-08-2009 at 09:44 AM..
|