473,839 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
+ 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 7396
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.selec tion) ...] will do the trick.
Apr 15 '08 #10

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

Similar topics

19
4837
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 code will trace these coordinates on the image and draw the line between these two points. Can anyone please help me in writing the javascript code of this problem? Thanks and Regards Atif
3
6683
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 Space key on the keyboard... But this is for a kiosk application with no keyboard :-( so I need to program a delete button. I have code that lets me insert text at the cursor position. And I can move the ABSOLUTE cursor position from the start and...
1
2888
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 the Transform method as follows float scale=2; Matrix x = new Matrix(); x.Scale(shift,shift); g.Transform=x;
0
1842
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 pixels from the left (or as near to there as possible based on the characters in the control). I basically have the position in which the user has positioned the mouse cursor within the textbox, and want to react as if they had actually clicked it....
4
19372
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 text - such as the date - is inserted into the textbox at the point where the caret is. (It's assumed that the user will be typing and then click the toolbar button.) From my research the textbox control doesn't seem to support any method of...
8
2361
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 point over the map. How can this be done in PHP ? I know you can do everything in GD, but does something similar already exist
3
21284
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' src='picture.gif' name='myimage' id='myimage'> </form> When forms with image types are submitted, the value is not returned; instead, when the form is submitted, the XY coordinates of the where the user clicked on the image are sent. For example, if the user
1
1981
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
8437
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 typing.) -Derik (OTOH, finding the Caret position-- between what characters the cursor is positioned-- uses the function below. I include it to be nice to future searchers, and because it took me an hour of frustrated
0
9855
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10649
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7829
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7018
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4487
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 we have to send another system
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.