Auto-Update: ON and $('#autoUpdate').toggle(functio...">

JQuery: adding .toggle to <a> makes it disappear

I have

<a id="autoUpdate">Auto-Update: ON</a>

      

and

$('#autoUpdate').toggle(function(){
alert('a');
}, function(){
alert('b');
})

      

When the page loads, the full <a>

-Tag is displayed: no and "b" is warned! I should add that this is a site under construction using Twitter Bootstrap if that matters, and the link is in the top nav bar. Btw if I change a-tag to a <p>

fe everything seems fine.

+3


source to share


1 answer


You are using a deprecated function .toggle

. It is removed in jQuery> = 1.9.0, so if you are using this version, this code is called instead of the animation function .toggle

, where the second function is the callback (and will be executed immediately in this case).

Instead, you must implement this functionality yourself:



var toggleState = true;
$('#autoUpdate').on("click", function() {
  if(toggleState) {
    alert('a');
  } else {
    alert('b');
  }
  toggleState = !toggleState;
});

      

+11


source







All Articles