oncontextmenu placement at mouse click

J

johkar

I am having trouble figuring out how I can get a custom context menu to
appear at the point of the mouse click. When the user right clicks
inside a text box, I want my custom menu to appear at that point. The
below kind of works unless the page scrolls and is scrolled down the
page a ways. I need the menu to be placed correctly regardless of
scrollbar position.

function setMenu(elm){
document.getElementById('myMenu').style.visibility='visible';
var x=document.body.scrollLeft + event.screenX;
var y=document.body.scrollTop + event.screenY;
document.getElementById('myMenu').style.left=x;
document.getElementById('myMenu').style.top=y;
}

<input type="text" name="textfield" oncontextmenu="setMenu(this);return
false" />

<div id="myMenu" style="position:absolute;visibility:hidden">
Some links here
</div>
Solutions or pointers for the major browsers appreciated.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top