PDA

View Full Version : Returning the children of the body tag.


enme
10.05.2002, 19:10
tester = function(){
this.tags = document.getElementsByTagName("body");
alert(this.tags.getElementsByTagName("*").length);
}
window.onload = tester;

Doesn't work... Anyone know why?

simu
10.05.2002, 19:25
document.getElementsByTagName("body") returns an array of all elements with the tag 'body'.

But this doesn't point directly to an element, you then have to pick out the right one and since I think your using just one body-tag, the element you want to refer to is:

document.getElementsByTagName("body")[0]

this should do the trick...

~ <font color=orange>Simon Käser</font id=orange>
~ [url:4dzatks3y0]mailto:admin@endlessX.com[/url:4dzatks3y0]
~ [url:4dzatks3y0]http://www.endlessX.com[/url:4dzatks3y0] coming soon

enme
10.05.2002, 19:29
Bloody hell! I fell like an idiot! I'm American and I said "bloody hell" hehe.

Brian Nickel
10.05.2002, 19:42
Properly typed, it should be "bloody 'ell" but I'm also american, so what do I know.

<font face='Courier New'><font color=brown>.Brian
It is best to remain silent and be thought a fool than to open your mouth and remove all doubt.</font id=brown></font id='Courier New'>

Chris
11.05.2002, 16:17
this works a treat:

<pre id=code><font face=courier size=2 id=code>
function getChildNodes(elm,elmToGet) {
var elm = document.getElementsByTagName(elm).item(0);
if(elm.hasChildNodes()!=-1) {
for (i=0;i<elm.childNodes.length;i++) {
if (elm.childNodes.item(i).nodeName==elmToGet) {
document.write(elm.childNodes.item(i).nodeName +"
");
}
}
}
}
getChildNodes('body','DIV');
</font id=code></pre id=code>

[url="http://chrispoole.com"]Chris[/url:z63zgwj0zi]

Edited by - Chris on 05/11/2002 15:21:52

mad hatter
12.05.2002, 01:15
Chris' function is almost good except for this part:

<pre id=code><font face=courier size=2 id=code>if(elm.hasChildNodes()!=-1)</font id=code></pre id=code>

hasChildNodes() returns either true or false, not a number. Besides, -1 is true in JavaScript (any number other than 0 is true), so the function would only run if there were no child nodes. It should be:

<pre id=code><font face=courier size=2 id=code>if( elm.hasChildNodes() )</font id=code></pre id=code>

Then it should work out fine. Also, the funtion isn't that useful for grabbing all the nodes of an element. Something more appropriate would be:

<pre id=code><font face=courier size=2 id=code>function getChildren(node, tagName) {
var i, j, out = new Array;
if( node && node.childNodes && node.hasChildNodes() ) {
for(i=0; i < node.childNodes.length; i++) {
j = node.childNodes.item(i);
if(tagName&& j.tagName == tagName)
out[out.length] = j;
else
out[out.length] = j;
}
}
return out;
}</font id=code></pre id=code>

Then you have the option of passing a tag name that the nodes have to have. Of course, the best way to get ALL the child elements of a node is just:

<pre id=code><font face=courier size=2 id=code>node.getElementsByTagName("*")</font id=code></pre id=code>

That would return everything, but you wouldn't get the structure. With a bit of modification, the getChildren() function could easily return a structured list of all the elements of a node, with or without a specified tag name.

Hope that helps a bit.

- David
<[url="http://www.stilleye.com"]<font color=black>stilleye</font id=black>[/url:ievh8mhqks]>
<[url="http://www.stilleye.com/scripts/marginfix"]<font color=black>IE Margin Fix</font id=black>[/url:ievh8mhqks]>

Chris
12.05.2002, 07:55
Dave: the function i made with this line:
<pre id=code><font face=courier size=2 id=code>
elm.hasChildNodes()!=-1
</font id=code></pre id=code>

does work perfectly. i've tried it, and it only runs if the element <u>has</u> child nodes... because i've made it run if it's not equal to -1, i.e. true...

and yes, the function you made would be a better alternative; i didn't think much about the function, only enough to get it working...

[url="http://chrispoole.com"]Chris[/url:j0rldshamj]

mad hatter
12.05.2002, 15:46
Chris,

You are lucky it works then. Because, as I said earlier, -1 is considered true in JavaScript.

So.... node.hasChildNodes() returns true or false (which can be considered "any non-zero number" or "0"). When you compare the result to -1, it does work because for most functions, true translates to 1 and false will translate to 0. So, in this case you are getting lucky but it is really bad practice to use integer values when trying to determine the truth or falsity or a statment. Just as it is bad practice to do something like:

ie = document.all ? 1 : 0;
if(ie);

You should be doing something like:

ie = document.all ? true : false;

It is much clearer that way and a lot more concrete. Not all programming languages allow you to use numbers (or the same numbers) for true/false tests. Clarity is key.

- David
<[url="http://www.stilleye.com"]<font color=black>stilleye</font id=black>[/url:zmp2l6y5ex]>
<[url="http://www.stilleye.com/scripts/marginfix"]<font color=black>IE Margin Fix</font id=black>[/url:zmp2l6y5ex]>

mad hatter
12.05.2002, 15:49
One more thing Chris, your function will break if there are no child nodes. When translated to numbers, the function returns either 0 or 1, so it will always return true when you test it to be != -1. To test things like this, just do something like:

num = node.hasChildNodes();
alert( Number(num) );

So this just goes along with not using numbers for true/false as you don't know what you'll get.

- David
<[url="http://www.stilleye.com"]<font color=black>stilleye</font id=black>[/url:nwbidj6v0q]>
<[url="http://www.stilleye.com/scripts/marginfix"]<font color=black>IE Margin Fix</font id=black>[/url:nwbidj6v0q]>

Garrett Smith
13.05.2002, 10:08
Testing the boolean equivalent of a number.

javascript:alert(Boolean(-1));
"true"

javascript:alert(Boolean(0));
"false"

javascript:alert(Boolean(-0));
"false"

//be careful here, a rounding error could
// change the value.
javascript:alert(Boolean(0.0));
"false"

javascript:alert(Boolean(-0.0));
"false"


Edited by - Garrett Smith on 05/13/2002 009:19:33

Chris
13.05.2002, 13:51
quite true Dave, i apologise :)

[url="http://chrispoole.com"]Chris[/url:fmg5o2am7o]

ttrenka
13.05.2002, 14:12
I believe the problem is an OS based one (the Boolean)....Microsoft Windows will always treat -1 as true (it's the equivient of all of the bits turned on, i.e. 11111111, including the first bit which denotes the sign) while other OSes tend to treat 1 as true.

To add to the mix, some OSes treat anything that is not zero as true, while others might treat everything other than -1 as false....joy and rapture.

I guess the point is, if there is a true Boolean construct in the language, you should always use that instead of the integer/bit equivilent, unless you feel like doing OS detection in a very sneaky way <img src=icon_smile_wink.gif border=0 align=middle>.

Later y'all

Tom Trenka
"wait a minute...I really am a Doctor <img src=icon_smile_big.gif border=0 align=middle>"