views:

256

answers:

3

I have a custom onclick handler for a block element (set up through jQuery's .click() method). This block element can contain links.

I'm fairly certain this is possible, so, how do I have the handler simply return if it was a link I clicked on (so that the link is visited without running my code)?

A: 

how about testing if the element is an anchor or not inside the on click function?

if(this.tagName() == "a")return true;

I didn't test it, check it and reply please

Kheu
or this.target or sumthing just a comment if that dont work :D
n00b32
+2  A: 

inside your click event, you can check what the event target tagname is:

$("#myDiv").click(function(e) {
  if (e.target.tagName.toLowerCase() == "a") {
    return true; //link clicked
  } else {
    //run your usual div click code here...
  }
});
peirix
Thanks! Forgot about the event being passed in. Silly me.
Ryan McCue
+5  A: 

Check the event object to see if the target is an anchor tag then return true:

$("div").click(function(eventObject){

 if($(eventObject.target).is("a"))
  return true;

 //run your code
});
Jataro
Didn't realise the .is() method existed, thanks!
Ryan McCue
Neither did I. +1 (:
peirix