473,386 Members | 1,864 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.

Getting next tag in list, nextSibling gives unexpected results

This is probably a no brainer for the most of you but my head is spinning at
the moment.

Considering the following list how do I get a reference to the ul just below
the li with id products? nextSibling gives me a reference to the #text# node
inside the li tag?

<ul>
<li id="products">products</li>
<ul>
<li id="child">child</li>
<li id="chair">chair</li>
</ul>
</ul>

I want to create several functions to add or remove items from the list.
Apr 21 '06 #1
6 2058
Marc wrote:
This is probably a no brainer for the most of you but my head is spinning at
the moment.

Considering the following list how do I get a reference to the ul just below
the li with id products? nextSibling gives me a reference to the #text# node
inside the li tag?
You could just use a while loop and call nextSibling until the nodeName
== 'ul'.

<ul>
<li id="products">products</li>
<ul>
<li id="child">child</li>
<li id="chair">chair</li>
</ul>
</ul>

--
Ian Collins.
Apr 21 '06 #2
Hi Ian,

Thanks for the quick response!

I tried doing that but I must be doing something utterly stupid:

var listItem = document.getElementById("products");
var col = listItem.childNodes;
var tmp = "";
for (var i=0;i<col.length; i++){
tmp += col[i].nodeName + "\n";
}
alert(tmp);

all I'm getting is #text#... nothing else :-(


"Ian Collins" <ia******@hotmail.com> schreef in bericht
news:4a*************@individual.net...
Marc wrote:
This is probably a no brainer for the most of you but my head is spinning at the moment.

Considering the following list how do I get a reference to the ul just below the li with id products? nextSibling gives me a reference to the #text# node inside the li tag?

You could just use a while loop and call nextSibling until the nodeName
== 'ul'.


<ul>
<li id="products">products</li>
<ul>
<li id="child">child</li>
<li id="chair">chair</li>
</ul>
</ul>

--
Ian Collins.

Apr 21 '06 #3
uhm... no I didn't try nextSibling...
I tried childNodes... :-/
"Marc" <so******@spam.com> schreef in bericht
news:17**************************@news.chello.nl.. .
Hi Ian,

Thanks for the quick response!

I tried doing that but I must be doing something utterly stupid:

var listItem = document.getElementById("products");
var col = listItem.childNodes;
var tmp = "";
for (var i=0;i<col.length; i++){
tmp += col[i].nodeName + "\n";
}
alert(tmp);

all I'm getting is #text#... nothing else :-(


"Ian Collins" <ia******@hotmail.com> schreef in bericht
news:4a*************@individual.net...
Marc wrote:
This is probably a no brainer for the most of you but my head is spinning at the moment.

Considering the following list how do I get a reference to the ul just below the li with id products? nextSibling gives me a reference to the
#text#
node inside the li tag?

You could just use a while loop and call nextSibling until the nodeName
== 'ul'.


<ul>
<li id="products">products</li>
<ul>
<li id="child">child</li>
<li id="chair">chair</li>
</ul>
</ul>

--
Ian Collins.


Apr 21 '06 #4
okay... got it... pfff...

var el = document.getElementById("products").nextSibling;
while (el) {
tmp += el.nodeName + "\n";
el = el.nextSibling;
}
alert(tmp);
need more coffee I gues... thanks for pointing me in the right direction!!!
"Ian Collins" <ia******@hotmail.com> schreef in bericht
news:4a*************@individual.net...
Marc wrote:
This is probably a no brainer for the most of you but my head is spinning at the moment.

Considering the following list how do I get a reference to the ul just below the li with id products? nextSibling gives me a reference to the #text# node inside the li tag?

You could just use a while loop and call nextSibling until the nodeName
== 'ul'.


<ul>
<li id="products">products</li>
<ul>
<li id="child">child</li>
<li id="chair">chair</li>
</ul>
</ul>

--
Ian Collins.

Apr 21 '06 #5
Marc wrote:
okay... got it... pfff...

var el = document.getElementById("products").nextSibling;
while (el) {
tmp += el.nodeName + "\n";
el = el.nextSibling;
}
alert(tmp);
need more coffee I gues... thanks for pointing me in the right direction!!!

Please don't top post, it's not considered good form.

It was one of many directions!

--
Ian Collins.
Apr 21 '06 #6
On 21/04/2006 09:09, Marc wrote:
Considering the following list how do I get a reference to the ul just below
the li with id products? [...]

<ul>
<li id="products">products</li>
<ul>


With potential difficulty. The markup is invalid, so how a browser
constructs the document tree is entirely dependant upon its error
correction mechanism. Any of the following are feasible results:

- <ul>
<li id="products">products
<ul> <!-- This is what the markup
should look like.
-->

- <ul>
<li id="products">products</li>
</ul>
<ul>

- <ul>
<li id="products">products</li>
<li> <!-- This would be a new list item. -->
<ul>

- <ul>
<li id="products">products</li>
<li id="child">child
<ul>

A browser might even tolerate the invalid markup, as-is. In any case,
one cannot hope for predictable behaviour with something that is
fundamentally broken. Fix the markup then - as has already been
suggested - use a loop (with the children [childNodes] of the 'products'
list item, in this case) until a list element is encountered.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 21 '06 #7

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
3
by: Wim Roffal | last post by:
I am having a <div> with <a>s and <div>s inside it. I am trying to read in all the elements in the right order. With mydiv.getElementsByTagName("a") I can get the elements of a specific tag...
6
by: hsomob1999 | last post by:
so i have a <ul> and I allow the user to append items to it. The problem is that on mozilla the <span class="line"> which is just a line to divide the sections gets overlaped and doesnt move down...
4
by: s99999999s2003 | last post by:
hi the database "execute" function returns a list of logical results. Each logical result is a list of row tuples, as explained in the documents. everytime i use it to execute various...
2
by: William Groombridge | last post by:
Alrighty, so if I have an XmlNodeList as the data source for a repeater, in my repeater, I would like to do things like: <%# DataBinder.Eval(Container.DataItem, "ChildNodes.InnerText") %> ...
2
by: microsoft | last post by:
I have a very "flat" doc structure like this <root> <one> <two> <three> ... n <=100 </root>
15
by: Christoph | last post by:
I'm trying to make it so that if the user sets focus to a form element (text), it automatically sets focus to the very next form element. This will, in effect, prevent the user from modifying the...
4
by: SM | last post by:
Hello, I have an unordered list similar to this one: <ul id=list> <li onclick="addParagraph(0)">Item 1</li> <li onclick="addParagraph(1)">Item 2</li> <li onclick="addParagraph(2)">Item...
54
by: bearophileHUGS | last post by:
Empty Python lists don't know the type of the items it will contain, so this sounds strange: 0 Because that may be an empty sequence of someobject: 0 In a statically typed language in...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.