Hi everybody,
I need to calculate caret coordinates within TEXTAREA in pixels.
Pointing me to the right direction will be appreciated.
Thanks
MaxIRMX
11 7343 acoder 16,027
Recognized Expert Moderator MVP pronerd 392
Recognized Expert Contributor
the task is to find CARET coordinates within textarea.
This is a reach, but you might try catching the onChange event from the textarea element in question, and checking the coordinates from the event. I am not sure if it would return the location of the carrot, or the mouse cursor.
If it does return the position of the carrot it would probably be the position on the page not the position inside the textarea. You could get the position in the text area though by subtracting the X, Y values from the position of the TextArea elment. The different should be the position inside the textarea.
acoder 16,027
Recognized Expert Moderator MVP
Sorry, it does not. I understand how to calculate object coordinates. However, the task is to find CARET coordinates within textarea. It looks like creating a range might work for IE but I could not find anything that worked for Firefox.
Can you post the code that seems to work in IE.
Firefox has a different model to IE for working with ranges/selections.
Can you post the code that seems to work in IE.
Firefox has a different model to IE for working with ranges/selections.
-
-
refT.caretPos = document.selection.createRange();
-
// refT is the TextArea object
-
...
-
// irrelevant stuff
-
...
-
posT=findPos(refT.caretPos);
-
posT[0]+=document.documentElement.scrollLeft;
-
posT[1]+=document.documentElement.scrollTop;
-
...
-
// posT now holds caret position within document
-
...
-
function findPos(obj)
-
{
-
var curleft = curtop = 0;
-
if (obj)
-
{
-
do
-
{
-
curleft += obj.offsetLeft;
-
curtop += obj.offsetTop;
-
} while (obj = obj.offsetParent);
-
}
-
return [curleft,curtop];
-
}
-
Non-IE browsers use the W3C Range/Mozilla Selection.
... and W3C Range/Mozilla Selection object attributes provide offset in characters relative to the selected node.
Thank's everybody. This one works for Firefox. It does not work for IE but I can manage it.
acoder 16,027
Recognized Expert Moderator MVP
Good to hear that you've something that works, though not sure why it wouldn't work in IE. However, since you have something working for IE, a little bit of feature detection [if (document.selection) ...] will do the trick.
According to the quirksmode link, the standard 'range' doesn't work in IE but does work in all the modern browsers.
acoder 16,027
Recognized Expert Moderator MVP
Yes, of course (as I mentioned earlier).
I would've thought that the linked code would have worked in IE too.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Atif |
last post by:
Hello all,
In my html page I want to add an image say of 800x600. Now I want that
when ever I am given two coordinates on this image say (x1, y1)=(50,
100) and (x2, y2)=(200, 300), the java script...
|
by: Noah |
last post by:
I have a text field in a form. I want the user to be able to click
a DELETE button and have the character at the cursor position deleted.
This would be just as if the user had pressed the Back...
|
by: phil cunningham |
last post by:
I am new to C# and GDI+ so I may have missed a simple way of doing this.
I am trying to use the mouse to pick object up by seeing if the mouse position is Inside the object.
However, I am using...
|
by: Oenone |
last post by:
I would like to set the caret position in a (single-line) textbox to a
position based on the number of pixels from the left of the control.
For example, I want the caret to be positioned 50...
|
by: Ben R. |
last post by:
Hi there,
I'm designing a winforms app in VB.NET 2.0 and have a textbox control and
also some toolbar buttons. I want to have it such that when the user clicks a
toolbar button, some predefined...
|
by: Bob Bedford |
last post by:
Hello,
I've some geographical coordinates and I'd like to put them on a map.
Example: I've the map of the USA. Once I've selected Chicago, I'd like a
point-cross or what ever, on the Chicago...
|
by: jackiepatti |
last post by:
QUESTION:
I have a web page containing a form that contains an image instead of a
submit button, e.g.
<form name='myform' action='get' method='otherpage.asp'>
<input type='image'...
|
by: =?Utf-8?B?QWxleCBLLg==?= |
last post by:
Hi all
How do I get current coordinates (X,Y) of the caret symbol in a multi-line
text box?
Thank you
|
by: ReGenesis0 |
last post by:
Is it possible to determine the caret position, in terms of x/y
pixels, within a textarea? (I want to have a suggestion box pop up
under where you're typing... so i need to determine where you are...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |