sauerburger
sauerburger

Reputation: 5138

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

Upvotes: 4

Views: 2951

Answers (4)

Puru
Puru

Reputation: 9083

OnClick event you can pass the current object type. something like

onclick=foo(this)

The function foo looks like

 function foo(obj) {    
      if(obj.tagName != 'A') {
       alert('Yes')    
      } 
}

Upvotes: 1

S L
S L

Reputation: 14318

$(function (){
  $('#link').click(function (e){ e.stopPropagation(); /*do other thing*/});
})

Upvotes: 1

istvan.halmen
istvan.halmen

Reputation: 3350

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

$('#link').click(function(e) {
  e.stopPropagation();
});

Upvotes: 8

ysrb
ysrb

Reputation: 6740

Use the unbind http://api.jquery.com/unbind/

Upvotes: 0

Related Questions