Click here to Skip to main content
15,885,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Code that shows mouse events
HTML
<div onmousemove="showCoords(event)" onmousemove="why()" onmouseout="clearCoor()">
</div>

<p id="demo"></p>
JavaScript
function showCoords(event) 
{
  var x = event.clientX;
  var y = event.clientY;
  var coor = "X coords: " + x + ", Y coords: " + y;
  document.getElementById("demo").innerHTML = coor;     
}

function clearCoor() 
{
  document.getElementById("demo").innerHTML = "";
}

function why(event) 
{
  var x = event.clientX;
  var y = event.clientY;
  if ((x == '40') && (y =='40'))
  {
    document.getElementById("what").innerHTML = "Wow";
  } 
}

function why() does not do what it should which is display message

What I have tried:

changed functions and added more features
Posted
Updated 24-Aug-21 3:38am
v6
Comments
four systems 24-Aug-21 8:36am    
https://www.w3schools.com/code/tryit.asp?filename=GTS6OXPZM7KU

another with vanilla js should display message with mouse co ordinates
Richard Deeming 24-Aug-21 8:52am    
1. Include the relevant parts of your code in your question. Do not simply link to another site with all of your code and expect people to visit it and dig through to find the problem.

2. Your question says the function wow doesn't do what it should. But there is no such function in your sample.
Richard MacCutchan 24-Aug-21 8:53am    
Where is the code for function wow?
Richard MacCutchan 24-Aug-21 9:01am    
You need to put your onmousemove in the body tag as shown in the sample link above:
<body onmousemove="why(event)" onmouseout="clearCoor()">

You also need to check the actual names you are using in the id fields of your HTML. What you have is inconsistent. A few simple corrections and your code works (sort of).
four systems 24-Aug-21 9:26am    
the function is why; its raining here

1 solution

This sort of works, so you can build on it yourself:
JavaScript
<!DOCTYPE html>
<html>
<head>
</head>

<body onmousemove="showCoords(event)" onmouseout="clearCoor()">
<script type="application/javascript">

  function showCoords(event) {
    var x = event.clientX;
    var y = event.clientY;
    var coor = "X coords: " + x + ", Y coords: " + y;
    document.getElementById("what").innerHTML = coor;
  }

  function clearCoor() {
    document.getElementById("what").innerHTML = "";
  }

</script>

<h2>A simple Javascript test</h2>

<p id="what">this is what</p>


</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900