Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Add a class to the parent li of .active using raw javascript

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31485
    Steven Gardner
    Participant

    Hi,

    I want to add a class name to the parent li item of li a.active



    I need to use raw javascript but I’m just starting out with javascript and this is taking me forever to work out.

    Any help would be much appreciated.

    Steve

    #62803
    Sirlon
    Member

    Well try


    var navlinks = document.getElementById('nav2').getElementsByTagName('a');
    for (var i = 0; i < navlinks.length; i++) {
    if(navlinks.className == 'active'){
    navlinks
    .parentNode.parentNode.parentNode.className = 'YOUR CLASS';
    }
    }

    Hope it works, have no idea when i did somthing like that without jquery the last time ^^

    #62336
    Steven Gardner
    Participant

    Cheers Sirlon,

    That code was what i almost got myself, I must be learning a bit.

    Thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.