Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2012, 02:29 AM   PM User | #1
BMAN99
New to the CF scene

 
Join Date: Nov 2010
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
BMAN99 is an unknown quantity at this point
How do you make onlick fire for ONLY the "top-most" div?

Hey guys,

Let's say I've got 2 divs, A and B, and each div has a separate onclick event. Div "A" is in the background, and div "B" is in the foreground, like this...

_______________________________________
|............................................................|
|........................Div A.............................|
|............................................................|
|..............___________________................|
|..............|............................|...............|
|..............|......... Div B............|...............|
|..............|__________________|...............|
|............................................................|
|............................................................|
|______________________________________|


What I want to do, is when div B is clicked on, it fires ONLY the onclick event for div B. But as it is, whenever I click on Div B, it fires the onclick event for Div B AND Div A.

So... how can I keep the div A onclick event from firing whenever I click on Div B?
BMAN99 is offline   Reply With Quote
Old 07-14-2012, 03:06 AM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
I think the correct answer is to give both divs a z-index, and give div-b a higher one. Can I put my hand down now?
DrDOS is offline   Reply With Quote
Old 07-14-2012, 03:34 AM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
the problem, I believe, is called event propagation... or bubbling... and CSS won't help you there. Have a look at the link and the code to stop it
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
BMAN99 (07-14-2012)
Old 07-14-2012, 08:50 AM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
</head>

<body>
<div style="position:relative;width:400px;height:400px;background-Color:red;" onclick="Test(1);" >
<div style="position:relative;left:100px;top:100px;width:200px;height:200px;background-Color:blue;" onclick="Test(2);" ></div>

</div>
<script type="text/javascript">
/*<![CDATA[*/
function Test(nu){
 var e=window.event||arguments.callee.caller.arguments[0]
 e.cancelBubble=true;
 if (e.stopPropagation){
  e.stopPropagation();
 }
 alert(nu);
}



/*]]>*/
</script></body>

</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Users who have thanked vwphillips for this post:
BMAN99 (07-14-2012)
Old 07-14-2012, 03:36 PM   PM User | #5
BMAN99
New to the CF scene

 
Join Date: Nov 2010
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
BMAN99 is an unknown quantity at this point
Vic,

Thank you so much... that's perfect!

And xelawho, thanks for the link... very informative!
BMAN99 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:58 PM.


Advertisement
Log in to turn off these ads.