Alex Zahir
Alex Zahir

Reputation: 969

Make button have active state on click

I want to make a button have a class .active added to it, to which i have attached CSS styles. I am trying to do this using jQuery.

My HTML is as follows:

    <ul class="num-list num-list-top">
        <li class="active"><a href="#">1</a></li>
        <li><a href="#">2</a></li>
       <li><a href="#">3</a></li>
    </ul>

My jQuery is as follows:

    $('.num-list-top li').click(function() {
        $(".num-list-top").toggleClass("active");
    });



Right now what the code does is give all the li list items a class of active. I want only the li item that is clicked to have the active class toggled.

Upvotes: 1

Views: 3778

Answers (1)

Wilfredo P
Wilfredo P

Reputation: 4076

Try:

$('.num-list-top li').click(function() {
        $(this).toggleClass("active");
    });

Becouse using this will get the li clicked

Upvotes: 2

Related Questions