Reputation: 4714
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
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
Reputation: 73896
Try this:
$('#divA').on('click', function() {
alert('divA is clicked');
});
$('#divB').on('click', function(e) {
e.stopPropagation();
alert('divB is clicked');
});
Upvotes: 4
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.
$('#divA').on('click', function(event) {
alert(event.target.id);
//Use event.target.id to check if divB inside divA is clicked also
});
Upvotes: 4