Cancel bubble event

Hi,

I have a div within a div. It seems the inner div receives message notifications first, which I'd then like to block from further bubbling to its parent div. Here's my setup:

    <div id = "div_parent" oncontextmenu="parentmenu();">

        <div id = "div_child" oncontextmenu="childmenu();">
        </div>

    </div>

    function childmenu()
    {
        // block oncontextmenu message from getting to parent now.
        if (window.event) {
            window.event.cancelBubble = false;
        }
        else {
            event.stopPropagation();
        }
    }

   This isn't working though, the parent div still tries to popup its context menu after the child first displays its menu. What must I change to get it to work?

Thank you        
LVL 7
minnirokAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

faifaiCommented:
try change to this for your // block oncontextmenu message from getting to parent now.

            if(ev.preventDefault)//firefox or those support preventDefault
                  {
                        ev.preventDefault();
                        ev.stopPropagation();
                  }
                  else if(window.event)//ie or those support returnValue and cancelBubble
                  {
                              event.returnValue=false;
                              event.cancelBubble=true;
                  }

hope this can help you

Carlton
0
minnirokAuthor Commented:
Hi Carlton,

It doesn't seem to be working, perhaps I used your suggestion incorrectly? Below I've pasted a fully working sample you can paste into an html file to see what I mean. If you right click the inner div box, its parent is still getting the contextmenu msg as well:

<html>
<head>

    <script>

    function menuparent()
    {
        alert("clicked parent");
    }

    function menuchild()
    {
        alert("clicked child");

        if(ev.preventDefault)//firefox or those support preventDefault
        {
            ev.preventDefault();
            ev.stopPropagation();
        }
        else if(window.event)//ie or those support returnValue and cancelBubble
        {
            event.returnValue=false;
            event.cancelBubble=true;
        }

    }
    </script>


</head>

<body>

<div id = "div1" oncontextmenu="menuparent();" style="position:absolute; width:200px; height:200px; left:100px; top:100px; border:1px solid black">

    <div id = "div2" oncontextmenu="menuchild();" style="position:absolute; width:100px; height:100px; left:50px; top:50px; border:1px solid black;">
    </div>

</div>

</body>


</html>
0
smaccariCommented:
For Firefox (and all browsers not using window.event), the event parameter is passed to the handler function.
Use this menuChild function:

function menuchild(ev)
    {
        alert("clicked child");

        if (window.event) {
            event.returnValue=false;
            event.cancelBubble=true;
        }
        else       {
            ev.preventDefault();
            ev.stopPropagation();
        }
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
minnirokAuthor Commented:
Cool that seems to have done it.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.