javascript - one button two different actions -
i'm new development section . have small clarification in jquery . single button should possess 2 different action . let consider button name pause/resume if click on pause button should alert pause clicking on same button has display resume .
var flag = false; $("#btn_pause_resume").click(function (){ if (flag) { alert("pause"); } else { alert("resume"); flag = true; }
my favorite way use attributes (data). kind of like:
<button data-paused="false"></button> here's solution:
$('#btn_pause_resume').click(function () { if ($(this).data('paused')==='false') { alert('resumed...'); $(this).data('paused', 'true'); } else { alert('paused...'); $(this).data('paused', 'false'); } }); demo
quick plugin
here's plugin wrote make easy:
$.fn.toggleclick=function(t,a,e){$(this).data("togglestate",e||false),this.click(function(){"false"===$(this).data("togglestate")?(t(),$(this).data("togglestate","true")):(a(),$(this).data("togglestate","false"))})}; add top of code , can do:
$('#btn_pause_resume').toggleclick( function () { alert('resumed!'); }, function () { alert('paused!'); }, true);//true makes second function run first demo
this adds toggleclick function. function takes 2 functions each run.
Comments
Post a Comment