Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click on link should not trigger parental onclick-event

I've got the following code:

<div onclick="alert('div event');" style="cursor:pointer">
    some text
    <a href="asd.php" id="link">click</a>
</div>

When somebody clicks on the link the javaschipt event is triggered. I want that the event is only triggers if somebody clicks on the text or on the empty space inside the div container; and not if somebody clicks on the link.

Is it possible to call a function when the event is triggered, which checks on which elements the user has clicked. something link

onclick="foo(caller);"

and

function foo(element){
    if(element!='link'){
        alert('yes');   
    }
} 
like image 505
sauerburger Avatar asked Feb 24 '23 14:02

sauerburger


2 Answers

Add a click handler to your link and stop event bubbleing to the parent div. Like this:

$('#link').click(function(e) {
  e.stopPropagation();
});
like image 193
istvan.halmen Avatar answered Mar 07 '23 10:03

istvan.halmen


$(function (){
  $('#link').click(function (e){ e.stopPropagation(); /*do other thing*/});
})
like image 37
Santosh Linkha Avatar answered Mar 07 '23 09:03

Santosh Linkha