zelazowy
zelazowy

Reputation: 1056

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

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

Answers (5)

Ram
Ram

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

http://jsfiddle.net/SmXCG/

Upvotes: 10

SarathSprakash
SarathSprakash

Reputation: 4624

Working DEMO

Try this

$('#page-container, .some-class').on('click', function(event) {
alert($(this).text());
});

Upvotes: 0

Arun P Johny
Arun P Johny

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

palaѕн
palaѕн

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

rajesh kakawat
rajesh kakawat

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

Related Questions