Forums

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

Home Forums JavaScript Add Class Not Working

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #195160
    wolfgang1983
    Participant

    When I click on my bootstrap menu link that has id of id=”dashboard” it should add class=”selected” and then makes it change color from css.

    How can I make it work correct? http://codepen.io/riwakawebsitedesigns/pen/gbXpPz

    http://codepen.io/riwakawebsitedesigns/pen/gbXpPz

    #195161
    Shikkediel
    Participant

    Just remove li from the selector…

    $("#dashboard").click(function() {
    
    #195162
    Paulie_D
    Member

    Your selector is incorrect

    $("li #dashboard")
    

    selects the #dashboard element that is a child of an li

    Since there will only be one #dashboard` the below should be adequate.

    $("#dashboard")
    

    http://codepen.io/Paulie-D/pen/EabjWg

    #195163
    Shikkediel
    Participant

    A minute late but a better answer. :-p

    #195164
    Paulie_D
    Member

    A minute late but a better answer

    I had the post ready but held off until I had the Codepen forked.

    I’d prefer toggleClass to addClass but each to their own.

    #195165
    wolfgang1983
    Participant

    When I click on id dashboard it disappears menu still need it to stay any idea why disappears

    #195166
    Paulie_D
    Member

    There’s a link inside the li so that’s also getting clicked so it tries to take you there.

    If you make the link a null # it works…I’ve updated the Codepen above.

    What do you want that click to do (other than be ‘selected’)?

    #195171
    wolfgang1983
    Participant

    On my on my menu when the user returns to the dashboard when click on link will show up. I will be adding a background url image for it I just put color red for testing.

    #195172
    Paulie_D
    Member

    On my on my menu when the user returns to the dashboard when click on link will show up. I will be adding a background url image for it I just put color red for testing.

    If the click of the link inside #dashboard navigates the user to a new page, then the .selected class will be lost when they return.

    Otherwise, it should be fine.

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