/    Sign up×
Community /Pin to ProfileBookmark

x-y position of cursor

Is there any way I can get the x- and y-position of the cursor?

What I mean is this:
I have a textarea:
<textarea cols=”5″ rows=”25″></textarea>

When I write “michelle” in the textarea and hit ” ” (spacebar), I want to call a function which gets the x- and y-position of the cursor.

Is this possible?

// Michelle

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreApr 16.2003 — This is the set of functions I use, but they have to be triggered by a mouse event...
[CODE]
function findXCoord(evt) {
if (evt.x) return evt.x;
if (evt.pageX) return evt.pageX;
}

function findYCoord(evt) {
if (evt.y) return evt.y;
if (evt.pageY) return evt.pageY;
}[/CODE]
Copy linkTweet thisAlerts:
@NicodemasApr 16.2003 — That returned only an error for me..
Copy linkTweet thisAlerts:
@NevermoreApr 16.2003 — Then you didn't use it correctly. Here is an example of how to use it:
[CODE]
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function findXCoord(evt) {
if (evt.x) return evt.x;
if (evt.pageX) return evt.pageX;
}

function findYCoord(evt) {
if (evt.y) return evt.y;
if (evt.pageY) return evt.pageY;
}
//-->
</script>
</head>

<body>
<a onclick="alert('The x coordinate is: ' + findXCoord(event) + ' and the y coordinate is: ' + findYCoord(event));">Hi</a>


</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@michelleauthorApr 16.2003 — So, there is no way I can get the x- and y-position of the cursor? You can insert text where the cursor is positioned, so I thought that you could get the positions for that place... Can't you do anything with focus or something?

The most usual problem I had with events is that I wrote <a href="javascript:function();">hi</a> instead of <a href="#" onClick="function();">hi</a>
Copy linkTweet thisAlerts:
@DrDaMourApr 16.2003 — what he was talking about was mouse pointer position, what you want seems to be the | line in a text box. That doesn't have an X and a Y value, but an offset from the start of the string. You could use a formula like: sizeofstring/columns = Y and sizeofstring%columns = X, you'd have to take care of some special cases though
×

Success!

Help @michelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...