julian
julian

Reputation: 4714

Check if it clicked when other element is clicked

Lets say I have divB inside divA. I have a click event on divA.
But how to check if divB inside divA is also clicked?

jsFiddle - http://jsfiddle.net/zwxK6/

The Script -

$('#divA').on('click', function() {
    //What to do here? how to check if divB inside divA is clicked also?
});​

Upvotes: 1

Views: 106

Answers (3)

Jai
Jai

Reputation: 74738

This will click the divA and then divB get clicked too.

You can find like this: http://jsfiddle.net/zwxK6/4/

function fireIt(id) {
    alert(id + " is clicked too.");
}
$(function() {
    $('#divA').on('click', function(e) {
       $(e.target.id).children('div').click(
          fireIt($(e.target).children('div').attr('id'))
       );
    });
});

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

Try this:

$('#divA').on('click', function() {
    alert('divA is clicked');
});

$('#divB').on('click', function(e) {
    e.stopPropagation();
    alert('divB is clicked');
});

DEMO HERE

Upvotes: 4

Adil
Adil

Reputation: 148110

Use event object to get the source of event, pass event object to function you are binding for click, event.target will give you source of event.

Live Demo

$('#divA').on('click', function(event) {
    alert(event.target.id);
    //Use event.target.id to check if divB inside divA is clicked also
});​

Upvotes: 4

Related Questions