473,320 Members | 1,722 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,320 software developers and data experts.

Caret coordinates in pixels

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 7326
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 Expert 256MB
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
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 Expert Mod 8TB
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 Expert 4TB
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 Expert Mod 8TB
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.