0

This is basically what i am trying to do:

$('body').on('click', '.class', function($(this));

I need a solution to make this work so that I can delegate the click event to all the elements with a particular class, but then pass the element that was clicked into a custom function that was written elsewhere. Since there are multiple instances of this element on the page, simply passing in the .class selector means they apply to all of those elements, but I need it to only act on the element that was clicked.

3 Answers3

2
$('body').on('click', '.class', function() {
    myCustomFunction(this);
});
DevlshOne
  • 8,357
  • 1
  • 29
  • 37
1

'this' within an event handler in jquery is a reference to the element that fired the event.

$('.class').click(function() { otherFunction(this); } );
taldonn
  • 186
  • 7
0
$('body').on('click', '.class', function(e){
    // the clicked element:
    var self = e.target;
    otherFunction(self);
});
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 1
    I would use e.currentTarget as I think its generally what folks assume will be returned. http://stackoverflow.com/questions/5921413/difference-between-e-target-and-e-currenttarget – urban_raccoons Jul 10 '13 at 22:21