4

I have a jquery code to set the click event as follow: $("#somediv").click(function() {alert('test')});

How do I remove the above click event? it seems that the .click() method will always append the exisiting ones.

user384080
  • 4,576
  • 15
  • 64
  • 96

2 Answers2

9

Use

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

If you only want to remove that function, you need a reference to it:

var test = function() {alert('test');};
$("#somediv").click(test);

window.setTimeout(function () {
    $('#somediv').unbind('click', test);
}, 10000);

http://api.jquery.com/unbind/

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • ok.. another question then.. how to remove the hover? it seems the unbind('hover') does not do the work – user384080 Jul 15 '10 at 00:22
  • 2
    hahahahha.. sorry found the answer from http://stackoverflow.com/questions/805133/how-do-i-unbind-hover-in-jquery – user384080 Jul 15 '10 at 00:23
0
You can use off() method as well.

on() will be used to create event and off() will be used to remove event.

function clickEvent() {
  $("#somediv2").show().fadeOut("slow");
};


To **remove** events you can use like this,

$('#somediv').off("click", "#somediv1", clickEvent);

To **add** events you can use like this,

$('#somediv').on("click", "#somediv1", clickEvent);

http://api.jquery.com/off/

http://api.jquery.com/on/