473,472 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Caret coordinates in pixels

5 New Member
Hi everybody,
I need to calculate caret coordinates within TEXTAREA in pixels.
Pointing me to the right direction will be appreciated.
Thanks
MaxIRMX
Mar 30 '08 #1
11 7343
acoder
16,027 Recognized Expert Moderator MVP
These two links should help:
http://www.quirksmode.org/js/findpos.html
http://www.quirksmode.org/dom/tests/offset.html
Apr 1 '08 #2
maxirmx
5 New Member
These two links should help:
http://www.quirksmode.org/js/findpos.html
http://www.quirksmode.org/dom/tests/offset.html
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.
Apr 1 '08 #3
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.
Apr 1 '08 #4
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.
Apr 2 '08 #5
maxirmx
5 New Member
Can you post the code that seems to work in IE.

Firefox has a different model to IE for working with ranges/selections.
Expand|Select|Wrap|Line Numbers
  1.  
  2.             refT.caretPos = document.selection.createRange();
  3.             // refT is the TextArea object 
  4.             ...
  5.             // irrelevant stuff
  6.             ...
  7.             posT=findPos(refT.caretPos);
  8.             posT[0]+=document.documentElement.scrollLeft;
  9.             posT[1]+=document.documentElement.scrollTop;
  10.             ...
  11.             // posT now holds caret position within document
  12.             ...
  13. function findPos(obj) 
  14. {
  15.     var curleft = curtop = 0;
  16.     if (obj) 
  17.     {
  18.         do 
  19.         {
  20.             curleft += obj.offsetLeft;
  21.             curtop += obj.offsetTop;
  22.         } while (obj = obj.offsetParent);
  23.     }
  24.     return [curleft,curtop];
  25. }
  26.  
Apr 13 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
Non-IE browsers use the W3C Range/Mozilla Selection.

See these links:
http://www.quirksmode.org/dom/range_intro.html
http://developer.mozilla.org/en/docs/DOM:range
You may also want to have a look at this for the window sizing properties.
Apr 14 '08 #7
maxirmx
5 New Member
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.
Apr 14 '08 #8
maxirmx
5 New Member
Thank's everybody. This one works for Firefox. It does not work for IE but I can manage it.
Apr 14 '08 #9
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.
Apr 15 '08 #10
drhowarddrfine
7,435 Recognized Expert Expert
According to the quirksmode link, the standard 'range' doesn't work in IE but does work in all the modern browsers.
Apr 15 '08 #11
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.
Apr 16 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

19
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...
3
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...
1
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...
0
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...
4
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...
8
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...
3
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'...
1
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
3
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...
0
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...
0
Oralloy
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,...
1
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...
0
tracyyun
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...
0
agi2029
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.