V
V
Vladislav Kalnaus2018-02-07 11:43:53
JavaScript
Vladislav Kalnaus, 2018-02-07 11:43:53

How to write a jQuery .on script correctly?

Please tell me there is a code

$('.list_punkt li.active').on('click', function(){
        $('.list_punkt').addClass('active');
        $('.list_punkt li.active').removeClass('active');
        $('.list_punkt li').on('click', function(){
            $(this).addClass('active');
            $('.list_punkt').removeClass('active');
        });
    });


Clicking on li.active opens the list and removes the class from this li. After clicking on another li, it is assigned the class active . The problem is this. That when you click the 2nd time, the list does not open and no action occurs. The script only runs once.

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kulaeff, 2018-02-07
@kulaeff

I assume you need something like this:

$('.list_punkt li').on('click', function() {
  $('.list_punkt li.active').removeClass('active');
  $(this).addClass('active');
});

If you provide the HTML code, it will be possible to give a more accurate answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question