How to change style in a last a element in a nav bar?

Discussion in 'JavaScript' started by Kinaski, Apr 25, 2008.

  1. #1
    Well, I'm a truly beginner with javascript, so I have trouble implementing even easiest DOM tricks with it. Actually I think I will place a very simple question here, but whatever, even this one makes me pull my hair out for all day long.

    I got this horiz. navigation on my page:

    <ul id="nav">
    <li><a href="#">/home</a></li>
    <li><a href="#">/services</a></li>
    <li><a href="#">/staff</a></li>
    <li><a href="#">/products</a></li>
    <li><a href="#">/contact</a></li>
    </ul>

    The nav is dynamic and the list items can change through back end CMS. I have a separators for each of the menu items inserted through css. What I wanna do now is to actually get rid of the last a item separator through javascript. How can I do this? How can I select exactly the last a item from the nav, so I could be able to insert inner style which will delete it's separator "background:none"?

    Thank you in advance!
     
    Kinaski, Apr 25, 2008 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    
    var menuLinks;
    
    if( document.getElementById('nav') && (menuLinks=document.getElementsByTagName('a')) )
     menuLinks[menuLinks.length-1].style.background='none';
    
    Code (markup):
     
    Logic Ali, Apr 25, 2008 IP
  3. Kinaski

    Kinaski Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but that doesn't work. Anyway I just found a working solution"
    
    document.getElementById("nav").getElementsByTagName("LI")[(document.getElementById("nav").getElementsByTagName("LI").length)-1].firstChild.style.backgroundImage = "none";
    
    Code (markup):
     
    Kinaski, May 9, 2008 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Just for the record that should have been:
    
    var menuLinks, menu;
    
    if( (menu=document.getElementById('nav')) && (menuLinks=menu.getElementsByTagName('a')) )
     menuLinks[menuLinks.length-1].style.background='none';
    Code (markup):
     
    Logic Ali, May 9, 2008 IP