Szabolcs Szűcs
Szabolcs Szűcs

Reputation: 49

Jquery addClass() is not working

I tried this code:

$(document).ready(function(){
    $('.dropdown-toggle').dropdown()
    $(function () {
        $('ul.vehicletypes li').click(function () {
            $('ul.vehicletypes li').removeClass('active');
            $(this).addClass('active');
        });
    });
});

but absolute not working! The .active class is working if I add manualy.

The html code

  

  <div class="vehicletype">
    <ul class="vehicletypes">
    	<li ><a href="index.php?vehicletype=car">Car</a><li>
        <li ><a href="index.php?vehicletype=van">Van</a><li>
     </ul>
  </div>
        

Upvotes: 1

Views: 98

Answers (3)

astrosixer
astrosixer

Reputation: 121

You have missed a semi-colon right after the dropdown() Please load the jquery library

 $(document).ready(function(){
        $('.dropdown-toggle').dropdown();
        $(function () {
            $('ul.vehicletypes li').click(function () {
                $('ul.vehicletypes li').removeClass('active');
                $(this).addClass('active');
            });
        });
    });

Please check this link:https://jsfiddle.net/5e29901t/10/

Upvotes: 0

pranav-dev
pranav-dev

Reputation: 68

Simply use this code.

$(document).ready(function(){
        $('ul.vehicletypes li').click(function () {
            $('ul.vehicletypes li').removeClass('active');
            $(this).addClass('active');
        });
});

Upvotes: 0

Christos
Christos

Reputation: 53958

The following

$(function () {
});

is equivalent to

$(document).ready(function(){
});

So I don't see any reason of why you have used it twice.

That you probably want is the following:

$(function () {
    // Here you attach a click handler for the click event on the
    // li elements of a ul.
    $('ul.vehicletypes li').click(function () {

        // You remove the active class
        $('ul.vehicletypes li').removeClass('active');

        // You add the active class to the currently clicked li element.
        $(this).addClass('active');
    });
});

Upvotes: 2

Related Questions