How Can I Bind An Event Handler To An Instance In Jquery?
I am trying to bind an event to a 'method' of a particular instance of a Javascript 'class' using jQuery. The requirement is that I in the event handler should be able to use the '
Solution 1:
Just use an anonymous function:
$("#myButton").click(function() { myCar.drive(); });
Solution 2:
Try this :
$("#myButton").each(function() {
var $btn = $(this);
$btn.on('click',function(){
// Do whatever you want.
});
});
Here you first create a loop to target all #myButton elements (Which is wrong in your example, You should be using Class instead) like:
$(".myButton").each(...
Then we attach the click event handler to all of them.
Post a Comment for "How Can I Bind An Event Handler To An Instance In Jquery?"