Mouseover and check if mousedown

let’s say i wana do something onmouseover=“somefunction(this)”

how do i check if mouse button is pressed down in function somefunction(this) ?

Hi xrado,

Welcome to SPF! :slight_smile:

Here’s one way:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Mouse Test</title>
<meta name='author' content='Mike Foster (Cross-Browser.com)'>
<style type='text/css'>
#d1 {
  color: #000;
  background: #9cc;
  padding: 20px;
}
</style>
<script type='text/javascript'>
var mouseIsDown = false;
window.onload = function()
{
  var e = document.getElementById('d1');
  e.onmouseover = divOnMouseover;
  document.onmousedown = docOnMousedown;
  document.onmouseup = docOnMouseup;
}
function divOnMouseover(e)
{
  report(e);
}
function docOnMousedown(e)
{
  mouseIsDown = true;
  report(e);
}
function docOnMouseup(e)
{
  mouseIsDown = false;
  report(e);
}
function report(evt)
{
  var ele, msg, ev = evt || window.event;
  ele = document.getElementById('type');
  ele.firstChild.nodeValue = ev.type;
  ele = document.getElementById('dir');
  ele.firstChild.nodeValue = mouseIsDown;
  ele = document.getElementById('coord');
  ele.firstChild.nodeValue = ev.clientX + ' : ' + ev.clientY;
  ele = document.getElementById('targ');
  ele.firstChild.nodeValue = (ev.target || ev.srcElement).tagName;
}
</script>
</head>
<body>
<h1>Mouse Test</h1>
<div id='d1'>
<p>Event Type: <span id='type'>&nbsp;</span></p>
<p>mouseIsDown: <span id='dir'>&nbsp;</span></p>
<p>Mouse Position: <span id='coord'>&nbsp;</span></p>
<p>Event Target tagName: <span id='targ'>&nbsp;</span></p>
</div>
<p>For more DHTML toys visit <a href='http://cross-browser.com/'>Cross-Browser.com</a></p>
</body>
</html>

tnx! interesting code