Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put cursor at end of text input's value

So, currently I have a text-input-field with a value that is also autofocused.

On page load, the value is selected / highlighted. Is there a way I can put the cursor at the end of the value text instead of highlighting it in javascript or CSS?

Here is a js fiddle where the autofocused text's value is highlighted: http://jsfiddle.net/TaGL5/

Here is the HTML code: <input type="text" value="value text" autofocus />

like image 768
Django Johnson Avatar asked Jul 22 '13 05:07

Django Johnson


People also ask

How do I keep my cursor in a TextBox?

To position the cursor at the end of the contents of a TextBox control, call the Select method and specify the selection start position equal to the length of the text content, and a selection length of 0.

How do you move the cursor to the end of Contenteditable entity?

selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range range. collapse(false);//collapse the range to the end point. false means collapse to end rather than the start selection = window. getSelection();//get the selection object (allows you to change selection) selection.

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 you set focus on input field?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.


1 Answers

This works for me

<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>
like image 173
Harsha Venkatram Avatar answered Sep 22 '22 17:09

Harsha Venkatram