473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to determine row/column of mouseclick in textarea

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.

Jan 16 '06 #1
5 3467
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.

Jan 16 '06 #2
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
Jan 16 '06 #3
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.

Jan 17 '06 #4
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
Jan 17 '06 #5
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.

Jan 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lloyd | last post by:
I am trying to insert text in a text area. I need to insert one set of values on the left side of the text area and have corresponding values on the left side of the text area in an even column....
3
by: glevik | last post by:
Hello, Anyone that can think of a way to programmaticaly determine the word on an HTML page that the user clicked on will be my hero for life. Leo
8
by: Ward B | last post by:
Greetings all. I'm getting the following error message... Unknown column 'icaodesc' in 'field list' when I try to update a table from a PHP form. What 'field list' is it referring to? My...
4
by: Peter Rilling | last post by:
What is the difference between the MouseClick and Click events?
4
by: Chris Dunaway | last post by:
I added a MouseClick event to a Button control. It responds properly to the Left click of the button, but not a middle click or right click. Is it possible to get a right click and/or middle...
6
by: Kristian Frost | last post by:
Hi, I'm trying to add, as you might guess, mouseclick listeners to the shapes I am drawing using the GDI+ commands in a similar sort of way as could be done with the old VB "shapes". Problem is,...
2
by: GS | last post by:
how can I tell if it is right mouse click from left mouseclick? I would also to test if a key like control is being held down at the same time. what I would really like to is to capture control...
5
by: mantrid | last post by:
Hello Im trying to find the correct syntax to use to determine whether which form element is visible on changing the value in a dropdown list I need something in the onChange of the dropdown...
3
by: Bill Nguyen | last post by:
I can't figure out how to identify/display the column header name in Mouseclick event. This is a checkbox column type and I would like all cells in this column to toggle checked/unchecked when the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.