tags:

views:

47

answers:

3

Hi, I am using a script by CSS-Tricks, which is pretty straightforward except my scenario includes a little tricky area :)

The script:

$(".myBox").click(function(){
   window.location=$(this).find("a").attr("href");
   return false;
});

This works perfectly except inside my DIV I have a span containing an icon that needs to trigger a tooltip but not link anywhere (already working fine).

My question: Is it possible for the above script to make the whole DIV clickable EXCEPT while hovering a child span?

Thank you!

+6  A: 

In your tooltip code you can stop the click from bubbling up using event.stopPropagation(), like this:

$(".myBox span").click(function(e){
  e.stopPropagation();
  //show tooltip
});

This stops the click event from going to the parent .myBox <div> from that tooltip <span>, meaning that your current .click() handler won't fire/redirect the page.

Nick Craver
Hi Nick thank you for the quick reply. Isn't this approach the other way around? Meaning the URL is on the span rather than the DIV?
ale
@ale - you'd use this in addition to your current code...when clicking in the span, nothing happens but the tooltip, when clicking anywhere else...the `click` bubbles up and the handler in your question works, make sense?
Nick Craver
Works like a charm :) cheers!
ale
Sorry one last question. Can I change the cursor to clearly show the whole area is a link?
ale
@ale - welcome! and welcome to SO! be sure to [accept answers](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) on this and future questions :)
Nick Craver
@ale - yup, just add a `cursor: pointer` CSS style to that `<div>`, for example: `.myBox { cursor: pointer; }`, possibly a `.myBox span { cursor: default; }` as well for the tooltip.
Nick Craver
@nick - cheers!
ale
A: 

You could the event target:

$(".myBox").click(function(e){
    if (e.target.nodeName.toUpperCase() !== 'SPAN') {
        window.location=$(this).find("a").attr("href");
        return false;
    }
 });

However, as Nick points out in the comments, this assumes that there will be no non-span children of the span. Since you are inserting a tooltip, Nick's solution is (inevitably) the better way to go.

lonesomeday
This only works if the `<span>` has no children.
Nick Craver
True. Edited to acknowledge the particular relevance of this point to a tooltip situation...
lonesomeday
What if the child span is turned into an child DIV with a specific class. Say the outer DIV has class .myBox, and the inner div containing the tooltip icon has class .myInfoBox?
ale
Then you'd need to do `if (0 === $(e.target).closest('.myInfoBox').length) {`
lonesomeday
A: 

I guess my question would be, does the span need to be a child of the div? I would put the span before or after the div tag, set its position to relative or absolute, then push it into the div tag. Like so:

<span style="position:relative;top:-20px;left:-20px">Icon</span>
<div> Do your div stuff here </div>

This should work. You might need to include a z-index of 1 or 2 for the span, but I doubt this. What this will do is put the div in behind, and the span in front; now the span won't react to your click handler. Hope this works!

bozdoz
Hi bozdoz that's a different way of looking at it. I will give it a try and see if it works. Cheers!
ale