Reputation: 5486
I am trying to get the image wherever the mouse is clicked. But getting this error:
Uncaught ReferenceError: evt is not defined onmousedown
HTML:
<html>
<head>
<title>Image move</title>
<script type="text/javascript" src="pos.js">
</script>
</head>
<body onmousedown="display(evt)">
<img id="myimage" height=300 width=300 src="pos.jpg" style="position:absolute;top:100px;left:100px"; alt="mypos" />
</body>
</html>
JS:
function display(evt)
{
var dom = document.getElementById("myimage");
dom.style.top = evt.clientY+"px";
dom.style.left = evt.clientX+"px";
}
Do I have to write event
explicitly? What's the problem?
Upvotes: 4
Views: 4110
Reputation: 46607
Just do onmousedown="display(event);"
. The active event is event
, not evt
.
When an event such as onmousedown
occurs, the piece of JavaScript associated with it gets executed in a scope that defines the event
object. Some browsers make it also available globally as window.event
, as elaborated by this response, meaning you wouldn't have to explicitly pass it to the display
function.
Upvotes: 5