PDA

View Full Version : Checking next element


subnet_rx
09-27-2006, 02:54 AM
I'm trying to run some code when a H2 element is clicked on an unordered list below it. The problem is, the code runs whether the h2 has a ul below it or not.


h[x].onclick = function(){
var ul = this.nextSibling;

while (ul.nodeType != 1){
ul = ul.nextSibling;
}



So, if I have something like this:


<h2>Subtitle</h2>
<h2>Subtitle 2</h2>
<ul><li>List</li></ul>


Clicking "Subtitle" runs the function on "Subtitle 2". I don't want the function to run unless the next element is an ul. How can I verify that the next element is an unordered list and if not, don't run anything?

Kor
09-27-2006, 11:38 AM
Check for the nodeName


while (ul.nodeName.toLowerCase()!='ul'){
ul = ul.nextSibling;
}

subnet_rx
09-27-2006, 03:35 PM
Well, I'm trying to wrap the entire block with this:

if(h[x].nextSibling.nodeName.toLowerCase()='ul'){
//run function
}


Because if the H2 element has a UL, then I'd like it to run, if not, move to the next H2 element. The function never runs though.

Basscyst
09-28-2006, 02:56 AM
if(h[x].nextSibling.nodeName.toLowerCase=='ul'){
//run function
}

Kor
09-28-2006, 09:27 AM
if(h[x].nextSibling.nodeName.toLowerCase()=='ul'){
//run function
}