6

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.

zelazowy
  • 1,016
  • 2
  • 12
  • 26

5 Answers5

10

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;
});

http://jsfiddle.net/SmXCG/

Ram
  • 143,282
  • 16
  • 168
  • 197
3

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
    });
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
2

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');
});
palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

Try

$('#page-container, .some-class').on('click', '.do-something', function(event) {
    var isSomeClass = $(this).closest('.some-class').length > 0
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Working DEMO

Try this

$('#page-container, .some-class').on('click', function(event) {
alert($(this).text());
});
SarathSprakash
  • 4,614
  • 2
  • 19
  • 35