Simon
Simon

Reputation: 199

jQuery slideToggle keeps repeating

I have a simple jQuery code that looks like this:

$( "#resp-navigation" ).one("click", function() {
    $( "#navigation" ).slideToggle(500);
});

My HTML looks like this:

<div id="resp-navigation"> ... </div>
<nav id="navigation"> ... </nav>

My problem is that when I click on the #resp-navigation div, the #navigation keeps toggling. Does anyone meybe know why?

Thanks.

Upvotes: 2

Views: 1095

Answers (3)

trevor
trevor

Reputation: 2300

If your intention is to have the '#navigation' div toggle each time someone clicks on the '#resp-navigation' div, then:

This: $( "#resp-navigation" ).one

Should be: $( "#resp-navigation" ).on

And then it works fine in this fiddle:

http://jsfiddle.net/us0dm62c/

If, however, your intention is to only ever have the '#navigation' div toggle once (so it disappears and cannot be shown again), then your code does exactly that with no modifications, as you can see in this fiddle:

http://jsfiddle.net/gktnhhm5/

Upvotes: 3

wherby
wherby

Reputation: 784

Because you are using .one(*). Using like $(".btn1").click(function(){...} will work for multiply click.

Upvotes: 0

jsleuth
jsleuth

Reputation: 618

As @trevor said, it should be on('click'), not one('click').

If you are seeing it ping ponging my guess is you have another on-click listener assigned elsewhere in the code that is doing the same thing.

Upvotes: 0

Related Questions