Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript setSelectionRange() method and cursor position

Tags:

javascript

I have the following piece of JavaScript for dynamically inserting unicode characters - represented below by the txtToIns variable - into a body of text

elmt.value = elmt.value.substring(0, elmt.selectionStart) + txtToIns + " " + elmt.value.substring(elmt.selectionStart, elmt.selectionEnd) + elmt.value.substring(elmt.selectionEnd, elmt.value.length) + " ";
elmt.focus();
elmt.value = elmt.value.trim();

With the above the cursor is always at the end of the text. Is there any way I can place it directly after txtToIns? And no, txtToIns.focus() won't work

like image 562
AndyW Avatar asked Apr 16 '12 19:04

AndyW


People also ask

How do I set the cursor position in input?

To move the cursor to the end of an input field: Use the setSelectionRange() method to set the current text selection position to the end of the input field. Call the focus() method on the input element. The focus method will move the cursor to the end of the input element's value.

What is setSelectionRange in Javascript?

setSelectionRange() method sets the start and end positions of the current text selection in an <input> or <textarea> element. Optionally, in newer browser versions, you can specify the direction in which selection should be considered to have occurred.

How do I get a caret position?

You also need the input's selectionDirection to get the caret position whenever selectionDirection is backward i.e. you make a selection left-ward in the textbox so that selectionStart is the caret, but when selectionDirection is forward the caret will be at selectionEnd.

How do I get the cursor position in HTML?

If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value.


2 Answers

I'm not sure what you're trying to do with the space characters in the textarea value, particularly the final one which will get trimmed off again two lines later, so I've ignored the spaces in my code below. I'm also not sure what you're trying to do with the text that was previously selected (if any), so I'm going to assume you want to overwrite it with the new text.

Bear in mind that this will not work in IE < 9, which does not support selectionStart, selectionEnd nor setSelectionRange().

Demo: http://jsfiddle.net/timdown/wPfVn/

Code:

var val = elmt.value;
var start = elmt.selectionStart, end = elmt.selectionEnd;
elmt.value = val.slice(0, start) + txtToIns + val.slice(end);
elmt.focus();
var caretPos = start + txtToIns.length;
elmt.setSelectionRange(caretPos, caretPos);
like image 82
Tim Down Avatar answered Oct 04 '22 09:10

Tim Down


When you change the value, the cursor will move to the end. You will have to manually reset the cursor position after you change its value.

Try this:

var start = elmt.selectionStart,
    len   = txtToIns.length,
    pos   = start + len;
elmt.value = elmt.value.substring(0, start) + txtToIns + " " + elmt.value.substring(start) + " ";
elmt.focus();
elmt.value = elmt.value.trim();
elmt.setSelectionRange( pos, pos, 0 );

I also cleaned up some unnecessary logic in the line where you set the value. Hopefully I didn't break anything, but if I did, just revert to your original line. ​

like image 41
Robert Messerle Avatar answered Oct 04 '22 09:10

Robert Messerle