JQuery toggle () function not working

This question may be asked earlier. But I can not find the error. This is from the new Boston video tutorial.

$(document).ready(function(){ $('#btn').toggle(function(){ $('#msg').html('yes'); },function(){ $('#msg').html('No'); }); }); 

A video showing that when you click the text div first, yes , and the next time it shows no. But when I try, when the page loads, the fadeout button and div show no . What for? There may be other methods to achieve this functionality. But why is this not working. It shows the switch syntax as

 $('#btn').toggle(function(){code},function{code}); 
0
javascript jquery html html5 toggle
Sep 15 '13 at 1:39 on
source share
1 answer

http://api.jquery.com/category/deprecated/

Note. This method signature is deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation method called .toggle () that switches the visibility of elements. Whether the animation or method is an event depends on the set of arguments passed.

http://api.jquery.com/toggle-event/

Updated after OP comment

Demo

if you want this code to work in any version of jQuery

 $(document).ready(function () { var arr = ['yes', 'no'], i = 0; $('#btn').click(function () { $('#msg').html(arr[i++ % 2]); }); }); 

Demo

if you want to use toggle-event functionality in the future

use .one ()

 $(document).ready(function () { function handle1() { $('#msg').html('yes'); $('#btn').one('click', handle2); } function handle2() { $('#msg').html('no'); $('#btn').one('click', handle1); } $('#btn').one('click', handle1); }); 

or

Toggle was removed in version 1.9 , so you cannot use it - if you want this logic to be implemented manually or you can use the migration plugin

enable migration plugin, after adding jQuery library add

 <script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script> 
+10
Sep 15 '13 at 1:40
source share



All Articles