Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Problem with comparing tagName in IF statement

    I have a Javascript function that is highlighting the menu in one of my courses. I am having a problem getting into an If statement in some cases. Here is my code:
    Code:
    function hilightCurrent() {
    	var nav = document.getElementById('nav');
    	if (nav == null) {
    		return;
    	}
    	
    	var checkIndex = hreftable[__currpage];
    	var aObj = nav.getElementsByTagName('a');
    	var found = false;
    	
    	while ((found == false) && (checkIndex > -1)){
    		for(j=0; j < aObj.length; j++) {
    			if(aObj[j].href.indexOf(index[checkIndex].href)>=0) {
    			  aObj[j].className='active';
    			  alert("parent tag = " + aObj[j].parentNode.parentNode.parentNode.tagName);
    			  if(aObj[j].parentNode.parentNode.parentNode.getElementsByTagName == 'li'){
    				  aObj[j].parentNode.parentNode.parentNode.className='active';
    				  aObj[j].parentNode.parentNode.parentNode.getElementsByTagName('a')[0].className='active';
    			  }
    			  found = true;
    			  break;
    			}
    		  }
    		  checkIndex--;
    	}
    }
    The alert I have in there currently is returning LI in the case where I want to get into the IF statement right below it. So the alert comes back as LI and then I am checking if it is LI but for some reason it doesn't get into that if statement. Can anyone help or have any ideas?

    Thanks!

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    tagName values are uppercase.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for the reply. I have tried changing it to:

    Code:
    if(aObj[j].parentNode.parentNode.parentNode.getElementsByTagName == 'LI'){
    				  aObj[j].parentNode.parentNode.parentNode.className='active';
    				  aObj[j].parentNode.parentNode.parentNode.getElementsByTagName('a')[0].className='active';
    			  }
    Unfortunately it still is not getting inside the IF. Any other ideas?

    Thanks!

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    Oops, I pasted the wrong code in my last post. I mean I tried changing to this:

    Code:
    if(aObj[j].parentNode.parentNode.parentNode.tagName == 'LI'){
    				  aObj[j].parentNode.parentNode.parentNode.className='active';
    				  aObj[j].parentNode.parentNode.parentNode.getElementsByTagName('a')[0].className='active';
    			  }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •