/    Sign up×
Community /Pin to ProfileBookmark

is there something like node.childElements?

I am working on a tab menu in JS, using the node.childElements collection. It works how I want in IE but is telling me that the node doesn’t have any properties. I figured out that this is because IE and FF handle white-space differently, so in FF, when I am looking for an element, it is actually giving me a textnode.

Is there a cood workaround to this? Something like a childElements collection perhaps (i.e. only giving full child elements but not text nodes)? Or do i need to write a function?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 30.2006 — you can

1. use getElementsByTagName() instead.

2. use a while loop and check the nodeType attribute of the childNodes

3. use a gapscleaner:
<i>
</i>&lt;script type="text/javascript"&gt;
var notWhitespace = /S/;
function cleanWhitespace(node) {
for (var x = 0; x &lt; node.childNodes.length; x++) {
var childNode = node.childNodes[x]
if ((childNode.nodeType == 3)&amp;&amp;(!notWhitespace.test(childNode.nodeValue))) {
// that is, if it's a whitespace text node
node.removeChild(node.childNodes[x])
x--
}
if (childNode.nodeType == 1) {
// elements can have text child nodes of their own
cleanWhitespace(childNode)
}
}
}

//clean whitespace on the whole document onload
//you may also clean on target nodes only
window.onload = function(){
cleanWhitespace(document);
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@UltimaterAug 31.2006 — Not exactly childElements but you can use [color=blue]getElementsByTagName("*")[/color] to collect all the tagNodes and ignore all the textNodes -- however unlike childNodes, getElementsByTagName includes subNodes.

Could use parentNode to detect subNodes and filter them out if it becomes an issue.
Copy linkTweet thisAlerts:
@aaronbdavisauthorAug 31.2006 — I ended up writing this function to get what I want.[code=php]/* get all child elements of a node (but not descendant nodes)
* and ignore text nodes */
var getChildElements = function(node)
{
var a = [];
var tags = node.getElementsByTagName("*");

for (var i = 0; i < tags.length; ++i)
{
if (node == tags[i].parentNode)
{
a.push(tags[i]);
}
}
return a;
}[/code]
Copy linkTweet thisAlerts:
@KorAug 31.2006 — hm... it works? I am used to think that push() method does not work in the particular case of the tags collection.

I prefere DOM methods to insert/remove elements in a DOM tree collection.
×

Success!

Help @aaronbdavis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...