Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selector on which 'click' event has been bound?

Tags:

jquery

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.

like image 224
zelazowy Avatar asked Sep 04 '13 10:09

zelazowy


3 Answers

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/

like image 149
undefined Avatar answered Oct 21 '22 09:10

undefined


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
    });
like image 24
rajesh kakawat Avatar answered Oct 21 '22 09:10

rajesh kakawat


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');
});
like image 2
palaѕн Avatar answered Oct 21 '22 08:10

palaѕн