Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove unbind attribute in jquery

I disabled the click event of image using unbind method. But I don't know how to recover the click event again. Here is the code,

<img src="testimg.jpg" id="sub_form">

disabled the click event of above image using the code

$('#sub_form').unbind('click');

How do i recover the click event? I tried with the bind event

  $('#sub_form').bind('click');

but it wont work.

Why I'm going for click event of image is ajax form submission. The code is,

$("#sub_form").click(function() {
var input_data = $('#testform').serialize();
    $.ajax({ 
//my code
});
});

how can i achieve this after unbind of image is performed.

like image 798
designersvsoft Avatar asked Dec 08 '25 21:12

designersvsoft


1 Answers

If you saved the handler, you can just invoke it again:

var handler = function() {
    alert('click');
};

$('#sub_form').click(handler);

// disabling:
$('#sub_form').unbind('click', handler);

// reenabling:
$('#sub_form').click(handler);

If you don’t know what handlers that are bound, you can find and save them before unbinding:

// save the click handlers
var events = $('#sub_form').data('events'),
    handlers = 'click' in events ? Array.prototype.slice.call(events.click) : [],
    handler = function() {
        $.each(handlers, function() {
            this.handler();
        });
    };

// disable
$('#sub_form').unbind('click');

// reenable
$('#sub_form').bind('click', handler);​

http://jsfiddle.net/sPPnE/

like image 142
David Hellsing Avatar answered Dec 10 '25 11:12

David Hellsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!