Reputation: 1056
I'm using jQuery on
function to bind click event:
$('#page-container, .some-class').on('click', '.do-something', function(event) {
// #page-container or .some-class?
});
Now inside callback function I want to know if 'click' event has been fired from #page-container
or .some-class
. With $(this)
I get only selector which has been clicked.
Upvotes: 6
Views: 6745
Reputation: 144689
delagateTarget
property of the event
object refers to the target of delegation:
$('#page-container, .some-class').on('click', '.do-something', function(event) {
var dt = event.delegateTarget;
});
Upvotes: 10
Reputation: 4624
Working DEMO
Try this
$('#page-container, .some-class').on('click', function(event) {
alert($(this).text());
});
Upvotes: 0
Reputation: 388316
Try
$('#page-container, .some-class').on('click', '.do-something', function(event) {
var isSomeClass = $(this).closest('.some-class').length > 0
});
Demo: Fiddle
Upvotes: 0
Reputation: 73906
You can do something like this:
$('#page-container, .some-class').on('click', '.do-something', function (event) {
if ($(this).closest('#page-container').length)
alert('Its #page-container');
else if ($(this).closest('.some-class').length)
alert('Its .some-class');
});
Upvotes: 2
Reputation: 10896
try something like this
$('#page-container, .some-class').on('click', '.do-something', function(event) {
$(this).attr('id'); // #page-container
$(this).hasClass('.some-class'); //Will return true if it has some-class
});
Upvotes: 3