473,461 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Retrieving Caret Start and End values

Jezternz
145 100+
Find the Caret starting and ending position in a textarea.

I have been looking for a function that will do this for a long time. Its amazing how many site claim to have a function that works, but does not. I would like to get it done here and hopefully others will find this post and not have to waste time. I have these simple requirements:
I input an ellement id (of a textarea in my case)
I get an output of both a start and an end value of the caret.
This must work in Ie6/Ie7/ff.
I have read numourous articles, and tried many functions all with their own problems.
The closest I got was one that worked perfectly however It would add the number of characters in multiple textboxes not just the one stated with its id inputted. Rendering it pretty useless.

This page has helped me get the closest but with problems still:
http://www.bazon.net/mishoo/articles.epl?art_id=1292


Thanks Heaps, Josh
Mar 24 '08 #1
4 2804
mrhoo
428 256MB
The caret's start position is the same index as its end position. Are you trying to capture a selection from the textarea?
Mar 24 '08 #2
Jezternz
145 100+
yes. Well to be exact I would like to know whether its a selection captured or its jut in a single place. I realise start = end wen it is in a single place. So Idealy I want 2 numbers.
Mar 24 '08 #3
Jezternz
145 100+
The closest one that I have is this:
Expand|Select|Wrap|Line Numbers
  1. function getSelectionStart(input) {
  2.     var range = document.selection.createRange();
  3.     var isCollapsed = range.compareEndPoints("StartToEnd", range) == 0;
  4.     if (!isCollapsed)range.collapse(true);
  5.     var b = range.getBookmark();
  6.     return b.charCodeAt(2) - 2;
  7. };
  8.  
  9. function getSelectionEnd(input) {
  10.     var range = document.selection.createRange();
  11.     var isCollapsed = range.compareEndPoints("StartToEnd", range) == 0;
  12.     if (!isCollapsed)range.collapse(false);
  13.     var b = range.getBookmark();
  14.     return b.charCodeAt(2) - 2;
  15. };
(from the link i previously posted)
and it works 100% fine except for example.
I have 2-4 selected, It will come out with 22-24 (note in this example the difference is 20, however this isnt always teh case its variable)... it always starts at strange values, it seems ot be counting other forms first or something.
Is there a way to modify it so it starts from 0 not eg 20.
thanks.
Mar 24 '08 #4
Jezternz
145 100+
Well, after reading testing and modifying functions from many different authors. I found one that works. I am leaving this here, incase anyone else has the same problem.
The coordinates are always correct no matter what text is entered into other fields on the page.
This will work in both ff and ie.

I cant remember the author sorry.
The function is as follows.
Expand|Select|Wrap|Line Numbers
  1. function getCaretPosition(textareaid){
  2.      var textarea = document.getElementById(textareaid);      
  3.     var startpos = 0;
  4.     var endpos = 0;
  5.     // For Firefox
  6.     if(document.getSelection){    
  7.         startpos = textarea.selectionStart;
  8.         endpos = textarea.selectionEnd;
  9.     // For Internet Explorer
  10.     }else if(document.selection){
  11.         var range = document.selection.createRange();
  12.         rangeCopy = range.duplicate();
  13.         rangeCopy.moveToElementText(textarea);
  14.         rangeCopy.setEndPoint( 'EndToEnd', range );
  15.         startpos = rangeCopy.text.length - range.text.length;
  16.         endpos = startpos + range.text.length;
  17.     }
  18.     return {start: startpos, end: endpos};
  19. }
  20.  
JS use example:
Expand|Select|Wrap|Line Numbers
  1. setTimeout("alert('From '+getCaretPosition('textid')['start']+' to '+getCaretPosition('textid')['end']);", 4000);
  2.  
Cool. Hopefully this will save someone hours of time trying to find a fucntion that works correctly.
Most of them either dont work with dynamic text before or after the element or do not return both start and end position.

Thanks Josh
Mar 25 '08 #5

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

Similar topics

2
by: - ions | last post by:
hi, i am in the procces of writing a chat program for a main Assigment, the problem im stuck on is one of the Carets position in a JTextArea, when i hit the enter key, it appends the msg to the...
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...
7
by: Paul Gorodyansky | last post by:
Hi, Say I have a text in my TEXTAREA box - 01234567890 I want - using script - insert say "abc" in the middle. Works almost OK in Internet Explorer (with one problem) based on their example...
4
by: cc | last post by:
Hi, Is possible set and get the caret position of cursor in a text field ? Thanks, Carlo
2
by: randy | last post by:
Hi, I'm trying to set up a richtextbox to behave like the text area in a web browser, i.e. no caret, and a cursor that changes depending on what it's hovering over. I've been setting the...
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: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
15
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
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
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...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.