Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,973
    Thanks
    15
    Thanked 229 Times in 229 Posts
    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?

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • Users who have thanked xelawho for this post:

    BMAN99 (07-14-2012)

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    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/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    BMAN99 (07-14-2012)

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Vic,

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

    And xelawho, thanks for the link... very informative!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •