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.
Looking at your question, you dont seem to bind back the
click
after youunbind
it, so it wont fire. (Assuming you've kept the sequence of your functionality right). You'll have to do it this way:When you're unbinding, call
unbind
with the function name:Then, to bind the click (when
#close
is clicked), you'd use :NOTE :
A better way would be use
on
andoff
, if youre using a version greater than jQuery v1.7 because, well.. it wont work. In the code above, just replacebind
withon
andunbind
withoff
.Hope this helps!
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.