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,006
  • 2
  • 12
  • 25

5 Answers5

9

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
  • 140,563
  • 16
  • 160
  • 190
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,698
  • 1
  • 20
  • 39
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ѕн
  • 68,816
  • 17
  • 108
  • 129
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
  • 376,738
  • 64
  • 519
  • 520
0

Working DEMO

Try this

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