Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

first unbind click and then bind (jquery)

1.I have a onclick event on,

$('#locations').click(function(){ $('#train').unbind('click');  //do some stuff } 

2.Once the close button is clicked

$('.close').click(function(){ //do some stuff } 

3.Then again if I click #train

$('#train').bind('click', function() { alert('train is clicked'); //do some stuff } 

Now the problem is #train is not firing.Is it to bind the event again on .close function?

Please suggest.Thanks in advance.

like image 401
Prithviraj Mitra Avatar asked Jun 22 '13 11:06

Prithviraj Mitra


People also ask

What is jQuery bind function?

The bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs. Syntax: $(selector).bind(event, data, function); Parameter: It accepts three parameters that are specified below-

Which method is used to remove Eventbind?

The unbind() Method is an inbuilt method in jQuery which is used to remove any selected event handlers. This method can be used to remove particular event handler, or stop specific functions. It works on any event handler using an event object.

What is bind and unbind in jQuery?

jQuery bind() function is used to attach an event handler to elements, while the unbind() is used to detached an existing event handler from elements.

How do you unbind a click event?

Use the off() method instead. The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.


2 Answers

Looking at your question, you do not seem to bind back the click after you unbind it, so it will not fire. (Assuming you've kept the sequence of your functionality right). You'll have to do it this way:

//Set a function with the action you need to do after you click the train function  trainClick() {   alert('train is clicked');   //do some stuff } 

When you're unbinding, call unbind with the function name:

$('#locations').click(function(){  $('#train').unbind('click',trainClick); //do some stuff } 

Then, to bind the click (when #close is clicked), you'd use :

$('.close').click(function(){   $('#train').bind('click',trainClick);   //do some stuff } 

NOTE :

A better way would be use on and off, if you are using a version greater than jQuery v1.7 because, well.. then it will not work. In the code above, just replace bind with on and unbind with off.

$('#train').on('click',trainClick); $('#train').off('click',trainClick); 

Hope this helps!

like image 121
krishwader Avatar answered Sep 24 '22 06:09

krishwader


BINDING AND UNBINDING HANDLERS

The Key is Scope.

You must declare and define the function (trainClick(){stuff it does}) outside the event-handler so that the other buttons' functions can see it.

Below is an example.

function trainClick() {     alert("train is clicked"); //Notice this function is declared outside event handlers below } $('#button1').on("click", trainClick); //this calls the above function  $("#button2").on("click",function(){     $("#button1").off("click",trainClick); //unbinds button1 from trainClick() function (event handler) });//End Button 2 click  $("#button3").on("click",function(){     $("#button1").on("click",trainClick); //binds button1 to trainClick() function (event handler) });//End Button 2 click 
like image 41
Sean Avatar answered Sep 26 '22 06:09

Sean