Remove unbind attribute in jquery

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

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

disabled click event over image using code



How do I restore the click event? I tried with bind event



but it won't work.

Why am I dispatching the click event for the image is an ajax form submit. Code:

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


how can i achieve this after deleting the image.


source to share

3 answers

If you've saved the handler, you can simply call it again:

var handler = function() {


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

// reenabling:


If you don't know which handlers are associated, you can find and save them before disconnecting:

// save the click handlers
var events = $('#sub_form').data('events'),
    handlers = 'click' in events ? : [],
    handler = function() {
        $.each(handlers, function() {

// disable

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



You need to provide your event handler with a function to run when an event occurs, try this:

$('#sub_form').bind('click', function() {
    alert("You clicked #sub_form");


If you are going to bind / decouple regularly, it would be better to put the logic in its own function so that it can be bounced easily:

$("#element").click(function() {

    // do something

    $("#sub_form").bind('click', myFunc);

function myFunc() {
    alert("You clicked #sub_form");




You can specify function reference

when calling .unbind()


For example:

function myHandler( event ) {

// bind the click handler
$('#sub_form').bind('click', myHandler);

// remove only this exact click handler
$('#sub_form').unbind('click', myHandler);

// bind it again
$('#sub_form').bind('click', myHandler);


Sidenote . As of jQuery 1.7.x, you must use the equivalent methods .on()

and .off()


Link: .on()

, .off()

, .bind()




All Articles