Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the next element

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

how do I get the next html element on the same tree-level? I mean:
if I have

<ul id="1">
<li="2">bla</li>
</ul>
<ul id="3">
<li>bla</li>
</ul>

starting from 1, I would like to get element 3. Does anyone has a hint? Thanks
 
[0] First not to assign id a numeric. It is no good, period. Also [tt]<li="2">[/tt] is not serious.

[1] Suppose you have this.
[tt]
<ul id="id1">
<li id="id2">bla</li>
</ul>
<ul id="id3">
<li>bla</li>
</ul>
[/tt]
[1.1] Starting from assumption you've got the reference to id="id1", via say getElementById() method. Then you can do this.
[tt]
var onode, otarget;
onode=document.getElementById("id1");
while (onode) {
onode=onode.nextSibling;
if (onode.nodeType==1) {
otarget=onode;
break;
}
onode.nextSibling;
}
if (otarget) {
//you actually have found one, and do something here
alert(otarget.id + "\n" + otarget.tagName); //just to verify
} else {
//you don't find one
}
[/tt]
 
amendment
I have misplaced a nextSibling line. Here is a retake.
[tt]
var onode, otarget;
onode=document.getElementById("id1");
[blue]onode=onode.nextSibling;[/blue]
while (onode) {
[red]//[/red]onode=onode.nextSibling;
if (onode.nodeType==1) {
otarget=onode;
break;
}
onode.nextSibling;
}
if (otarget) {
//you actually have found one, and do something here
alert(otarget.id + "\n" + otarget.tagName); //just to verify
} else {
//you don't find one
}
[/tt]
 
amendment-2
Sorry.
[tt]
var onode, otarget;
onode=document.getElementById("id1");
[blue]onode=onode.nextSibling;[/blue]
while (onode) {
[red]//[/red]onode=onode.nextSibling;
if (onode.nodeType==1) {
otarget=onode;
break;
}
[red]onode=[/red]onode.nextSibling;
}
if (otarget) {
//you actually have found one, and do something here
alert(otarget.id + "\n" + otarget.tagName); //just to verify
} else {
//you don't find one
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top