How to check weather a node has sibling

M

Moses

Hi All,

How to check weather a node has sibling? Is there any function
like " hasChildNodes() "

Thanks in Advance
Moses
 
E

Evertjan.

Moses wrote on 19 mrt 2007 in comp.lang.javascript:
How to check weather a node has sibling? Is there any function
like " hasChildNodes() "

<div>
<span id="s">x</span>
<span>x</span>
<span>x</span>
<span>x</span>
</div>


<script type='text/javascript'>

var d = document.getElementById('s')
var numberOfSiblings = d.parentNode.childNodes.length
alert(numberOfSiblings) // 8 in IE7, 9 in FF2. >0 is hasSiblings

</script>

Read up here:
<http://www.pageresource.com/dhtml/ryan/part4-1.html>
 
P

Patient Guy

Moses said:
Hi All,

How to check weather a node has sibling? Is there any function
like " hasChildNodes() "


function hasAnySibling(node)
{
if (node.previousSibling == null && node.nextSibling == null)
return (false);
return (true);
}
 
E

Evertjan.

Patient Guy wrote on 19 mrt 2007 in comp.lang.javascript:
function hasAnySibling(node)
{
if (node.previousSibling == null && node.nextSibling == null)
return (false);
return (true);
}

Nice!

But why the if construct and all the ()'s?

function hasAnySiblings(node) {
return !!node.previousSibling || !!node.nextSibling;
};
 
M

Moses

Hi All

Thanks for your repelys,

I need to check weather a node has next sibling so I tried
if(node.nextSibling == null) it works for IE but for FF it always
return true, because FF takes white space as a node. So I have
rewritten the function.


function hasNextSibling(node)
{
var node =node.nextSibling;
if(node==null){//For IE
return (false);
}
else{//For FF
while (node.nodeType!=1){
node=node.nextSibling;
if(node == null)
break;
}
if (node == null){
return (false);
}
else{
return (true);
}
}

}

Regards
Moses
 
E

Evertjan.

Moses wrote on 19 mrt 2007 in comp.lang.javascript:
I need to check weather a node has next sibling so I tried
if(node.nextSibling == null) it works for IE but for FF it always
return true, because FF takes white space as a node. So I have
rewritten the function.

Good detective work!

function hasNextSibling(node)
{
var node =node.nextSibling;
if(node==null){//For IE
return (false);

No need for the parentheses
}
else{//For FF

no need for else, if the first if-effect exits the function
while (node.nodeType!=1){
node=node.nextSibling;
if(node == null)
break;
}

===============

This part:
if (node == null){
return (false);
}
else{
return (true);
}

[No need for the parentheses around true and false]

.... is the same as:

return node != null;

So the whole function can be written as:

function hasRealNextSibling(node) {
var node = node.nextSibling;
if (node == null) return false; //For IE
while (node.nodeType != 1){
node = node.nextSibling;
if (node == null) break;
};
return node != null;
};

[However, I believe that an empty <span> is not detected right.]
 
R

RobG

Hi All

Thanks for your repelys,

I need to check weather a node has next sibling so I tried
if(node.nextSibling == null) it works for IE but for FF it always
return true, because FF takes white space as a node. So I have
rewritten the function.

function hasNextSibling(node)
{
var node =node.nextSibling;
if(node==null){//For IE
return (false);
}
else{//For FF
while (node.nodeType!=1){
node=node.nextSibling;
if(node == null)
break;
}
if (node == null){
return (false);
}
else{
return (true);
}
}

}

Consider:

function hasNextSibling(node) {
do {
node = node.nextSibling;
} while (node && node.nodeType != 1)
return !!node;
}

But consider fully the remifications of ignoring #text nodes, e.g.

<body>
<div id="xx">a div</div>
Here is some text
</body>

hasNextSibling() will report xx has no nextSibling when clearly it
does.
 
E

Evertjan.

RobG wrote on 19 mrt 2007 in comp.lang.javascript:
Consider:

function hasNextSibling(node) {
do {
node = node.nextSibling;
} while (node && node.nodeType != 1)
return !!node;
}

But consider fully the remifications of ignoring #text nodes, e.g.

<body>
<div id="xx">a div</div>
Here is some text
</body>

hasNextSibling() will report xx has no nextSibling when clearly it
does.

That's easily remedied with renaming the function to:

hasNextElementSibling()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top