473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to check weather a node has sibling

Hi All,

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

Thanks in Advance
Moses

Mar 19 '07 #1
9 18634
On Mar 19, 6:48 pm, "Moses" <mosesdinaka...@gmail.comwrote:
Hi All,

How to check weather a node has sibling? Is there any function
like " hasChildNodes() "
<URL: http://www.w3.org/TR/DOM-Level-2-Cor...l#ID-810594187 >

Some other handy links:

<URL: http://www.w3.org/TR/DOM-Level-2-Core/def-index.html >
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/def-index.html >
--
Rob

Mar 19 '07 #2
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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 19 '07 #3
"Moses" <mo************@gmail.comwrote in comp.lang.javascript:
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);
}

Mar 19 '07 #4
Patient Guy wrote on 19 mrt 2007 in comp.lang.javascript:
"Moses" <mo************@gmail.comwrote in comp.lang.javascript:
> 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);
}
Nice!

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

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 19 '07 #5

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
Mar 19 '07 #6
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 <spanis not detected right.]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 19 '07 #7
Great :)

Mar 19 '07 #8
On Mar 19, 10:21 pm, "Moses" <mosesdinaka...@gmail.comwrote:
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.
--
Rob

Mar 19 '07 #9
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()

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: midmukesh | last post by:
Hi, I am Mukesh, I have prepared an web application in ASP.NET 2003, And I want that, when user / client will open the link of my site, the system of that user will automatically be checked and...
1
by: mosesdinakaran | last post by:
Hi, Is there a way to check weather a variable contains serialized data or not, as we hve some functions like ( is_ double,is_ float) to check the integer and float value. Moses
4
gundarap
by: gundarap | last post by:
Hello all, I'm working on minidom. My goal is to see whether an element already exists in the xml file before adding. I was using getElementsByTagName() to check weather the element already exists....
0
by: Janarthanan Lakshmanan | last post by:
Hi to all, We have a treeview control in ASP.Net 2.0.We used in our project.We faced some problem while check checkbox. We want to check the parent node check box, if the respective child node was...
1
by: Sylvie | last post by:
I have a treeview on my web form, When I check one of the parent nodes, Child nodes must be checked automatically at client Is this possible ?
1
by: Sylvie | last post by:
I have a treeview on my web form, When I check one of the parent nodes, Child nodes must be checked automatically at client Is this possible ?
2
by: =?Utf-8?B?bWFib25k?= | last post by:
I'm using a treeview control. One parent node, several child nodes, several grandchild nodes. Each of the nodes has a checkbox. My question, when the user changes the check value of one of the...
0
by: bharathreddy | last post by:
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command. Using the system tables...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.