Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to determine row/column of mouseclick in textarea

Reply
Thread Tools

How to determine row/column of mouseclick in textarea

 
 
James Black
Guest
Posts: n/a
 
      01-16-2006
I am tracking the mouse movements in the textarea, but it gives me
mouse coordinates.

I use this code, for the moment, to get the mouse coordinates:
xcoord = e.pageX - e.target.offsetLeft;
ycoord = e.pageY - e.target.offsetTop;


I want it to be relative to the text area, not to the screen, which is
why I do the subtraction.

I am using FF1.5 for my testing, at the moment.

How do I determine the row/col?

Thank you.

 
Reply With Quote
 
 
 
 
impaler
Guest
Posts: n/a
 
      01-16-2006
The text area might be styled. You have to extract all the data (font
type, width, spacing etc) and do some nasty calculus. If it's not a
fixed font, you might not even succeed. Perhaps I'm wrong but it's
scary anyhow Hey, but nothing is impossible.

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-16-2006
James Black wrote:

> I am tracking the mouse movements in the textarea, but it gives me
> mouse coordinates.


"But"?

> I use this code, for the moment, to get the mouse coordinates:
> xcoord = e.pageX - e.target.offsetLeft;
> ycoord = e.pageY - e.target.offsetTop;


You have to take the offsetLeft and offsetTop of the target's parent
and its ancestors into account.

> I am using FF1.5 for my testing, at the moment.
>
> How do I determine the row/col?


You cannot, since you cannot know the font actually used.


PointedEars
 
Reply With Quote
 
James Black
Guest
Posts: n/a
 
      01-17-2006
I will modify the code to take into account the target's parents, etc,
into account.

I will also see about using a fixed font and see if I can do some math
to figure it out.

Thanx.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-17-2006
James Black wrote:

> [...]
> I will also see about using a fixed font and see if I can do some math
> to figure it out.


As I said, the attempt is futile. Even if you say that a specific font
should be used, if that font is not available a compliant UA will choose
but a similar one or a default font if no similar font is available. And
there is display zoom, font zoom etc.


PointedEars
 
Reply With Quote
 
James Black
Guest
Posts: n/a
 
      01-17-2006
I have a method that appears to work, but needs more testing:

charHeight = elem.clientHeight / elem.rows;
charWidth = elem.clientWidth / elem.cols;

elem was initialized by getting the textarea element.

var col = parseInt(xcoord / charWidth);
var row = parseInt(ycoord / charHeight);

The xcoord and ycoord is the x and y coordinates relative to the
textarea.

I am trying to determine which word was clicked on, which is why I need
the col/row, so I am not looking for perfection, but for extremely
close.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Catching MouseClick events jediknight ASP .Net 2 07-04-2006 03:01 PM
[JNI] Catch Ctrl+Mouseclick on Windows? watzlaw wutz Java 2 05-11-2004 06:16 PM
RE: winapi: mouseclick Tim Golden Python 1 01-27-2004 04:52 PM
winapi: mouseclick KNS Python 1 01-27-2004 08:19 AM



Advertisments