Click to See Complete Forum and Search --> : x-y position of cursor
michelle
04-16-2003, 06:11 AM
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
Nevermore
04-16-2003, 06:18 AM
This is the set of functions I use, but they have to be triggered by a mouse event...
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;
}
Nicodemas
04-16-2003, 06:25 AM
That returned only an error for me..
Nevermore
04-16-2003, 06:47 AM
Then you didn't use it correctly. Here is an example of how to use it:
<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>
michelle
04-16-2003, 07:14 AM
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>
DrDaMour
04-16-2003, 07:40 AM
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