319 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Exclusive Author
  • Portugal
vtimbuc says

Hey guys,

I need to make a simple script to toggle a div on the click event.

The problem is that the function only runs two time. Hides the div, then shows, after that stops working. I don’t know whats going on, it’s the first time that happens to me.

$(document).ready(function() {
    $('.search-tools-toggle').on('click', function(event) {

        if ($('#search-toggle').hasClass('hide')) {
            $('#search-toggle').slideDown().removeClass('hide');
        } else {
            $('#search-toggle').slideUp();
        }

    });
});

Am I doing something wrong?

Thanks

1400 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
  • Croatia
OriginalEXE says

It’s your logic bug, not jQuery bug :)

You are forgetting to add ‘hide’ class again when you slideUp, so this would be the correct code:

$(document).ready(function() {
    $('.search-tools-toggle').on('click', function(event) {

        if ($('#search-toggle').hasClass('hide')) {
            $('#search-toggle').slideDown().removeClass('hide');
        } else {
            $('#search-toggle').slideUp().addClass('hide');
        }

    });
});
319 posts
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Referred between 100 and 199 users
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Exclusive Author
  • Portugal
vtimbuc says

Ehehe, I’m too tired, it’s time to go to bed :)

Thank you @OriginalEXE!

PS: Moderators please close :)

by
by
by
by
by
by