How to assign a Css class on an item based on click


Tags: jquery,html,css

Problem :

Fiddle: http://jsfiddle.net/57f78yx1/1/

JQuery:

$(function() {
    $(".menu ul li").on("click", function(e) {
        alert($(this).index());
        var loe = $(this).index();
        $(".menu ul li:nth-child(" + loe + ")").addClass("activeLI");
    });
});

How can I modify so onclick set the style to the clicked item to activeLI and remove from the rest.



Solution :

Remove the class from all .menu ul li and add it only to $(this) (the clicked element)

http://jsfiddle.net/57f78yx1/2/

$(function() {
    $(".menu ul li").on("click", function() {
        $(".menu ul li").removeClass("activeLI");
        $(this).addClass("activeLI");
    });
});

    CSS Howto..

    CSS How to animate a transition from opacity 0 upon hover

    How to make an image not overlap navigation bar in CSS/HTML [closed]

    How to get cards with his content perfectly alligned

    Bootstrap How-to: checkbox should change text input value when selected/checked [closed]

    How to control CSS cache?

    How to make a div box with display:flex clickable like display:block?

    Meteor how to serve multiple css for different media types?

    How to target entire Bootstrap navbar

    How do I have a div go around another div?

    How Should I Send HTML/CSS Content To A Server?

    How to do mutiple css class in one line javascript code

    how to create divs rowwise in css

    CSS: how to layout 3 columns, 2 are optional

    How many significant figures can a number have in CSS?

    How to make my text appearing while scrolling website

    Selenium: How to get text from within an html tag which has another tag in it

    how to get 2 div's text to align along the bottom?

    How to set the CSS content property with a Google Material Icon?

    How can I get drop-down functionality working with CSS?

    How to center inline blocks together with text vertically without space at bottom?

    How to center part of a navbar

    How to create a two vertically stacked buttons next to a text input

    How to make a photo collage using html and css? [closed]

    How to align div first left then top in div parent?

    How to move items left and right in an html table using css? -

    How do I hide all children except the for the first child in Javascript/JQuery and CSS?

    How to pause .setInterval() on hover and on .click() of interior content?

    How to develop Desktop Apps using HTML/CSS/JavaScript? [closed]

    how to align 5 div side by side in center using css?

    How to make a background text appear under youtube video?