potato
potato

Reputation: 367

My function to find the x coordinate of the mouse doesn't work

I set document.body.onmousemove to the following function so that the console will log the X coordinate of the mouse on the page whenever the mouse is moved inside the body.

document.body.onmousemove = "function mouseX(e){console.log(e.clientX);}(event)";

This doesn't work. It produces no errors, but nothing happens. When I check the value of document.body.onmousemove I get null.

Note that I want to have this script injected through the JavaScript console, so I don't want html code and I would prefer not to use any JavaScript libraries either.

Upvotes: 0

Views: 68

Answers (1)

Zaenille
Zaenille

Reputation: 1384

Use document.body.onmousemove = function mouseX(e){console.log(e.clientX);}

You were assigning a string to document.body.onmousemove when you should have been assigning a function that you would like to fire when the mouse actually moves.

Upvotes: 2

Related Questions