Web Analytics Made Easy -
StatCounter Checking next element - CodingForum

Announcement

Collapse
No announcement yet.

Checking next element

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking next element

    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.

    Code:
    		h[x].onclick = function(){
    			var ul = this.nextSibling;
    			
    			while (ul.nodeType != 1){
    				ul = ul.nextSibling;
    			}
    So, if I have something like this:

    Code:
    <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?
    Gamecron PC Gaming Blog
    Bonsai Blog

  • #2
    Check for the nodeName

    Code:
    while (ul.nodeName.toLowerCase()!='ul'){
    ul = ul.nextSibling;
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Well, I'm trying to wrap the entire block with this:
      Code:
      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.
      Last edited by subnet_rx; Sep 27, 2006, 10:47 AM.
      Gamecron PC Gaming Blog
      Bonsai Blog

      Comment


      • #4
        Code:
        if(h[x].nextSibling.nodeName.toLowerCase=='ul'){
        //run function
        }
        Helping to build a bigger box. - Adam Matthews

        Comment


        • #5
          Code:
          if(h[x].nextSibling.nodeName.toLowerCase()=='ul'){
          //run function
          }
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment

          Working...
          X