Friday, 30 October 2015

How to bind, unbind and rebind (click) events in JQuery




HTML:
<a id="button" href="#">click</a>
<br><br>
<a id="toggle" href="#">unbind</a>

Script:
$(document).on('click', 'a#button', function(){
    $(this).after('<span> hello</span>');
    $('span').fadeOut(1000);
});

$('a#toggle').toggle(
    function(){
        $(this).text('rebind');
        $('a#button').on('click.disabled', false);
    },
    function(){
        $(this).text('unbind');
        $('a#button').off('click.disabled');
    }
);


2 comments: