There is a container that should appear at the click of a button. I wrote such code and everything seems to be fine, everything works, but one thing is “But.” When clicked, flips to the top of the page. Is it even legal?

$('.price .table__container').hide(); $(".btn-green").click(function(){ $(this).next(".table__container").slideToggle(500); $(this).toggleClass("activeBtn"); }); 

    2 answers 2

    Add the following code to the function called by click:

     event.preventDefault(); 

    Thus, the standard action will not be triggered.

      I bet that .btn-green is a link. When clicked, it adds anchor # or refreshes the page. To prevent this from happening, add return false to the end of the function. return false disables other actions that occur after pressing the button:

       $('.price .table__container').hide(); $(".btn-green").click(function(){ $(this).next(".table__container").slideToggle(500); $(this).toggleClass("activeBtn"); return false; }); 
      • @ Danil, I updated the answer and painted everything - Yuri