Questions? Call (860)407-2687
find us on:


Script፦ How To Place Cursor ⌶ In Input Like Textbox Or Textarea ▭

short url:

Javascript: How To Place Cursor ⌶ In Input Element Like Textbox Or Textarea ▭

Click to Play Video About Javascript፦ Put Mouse Cursor ⌶ In Input Element ▭
  • JS to Put Mouse Cursor in Input Text or Textarea
  • JS to Put Cursor at a SPECIFIC POSITION in Input Textbox or Textarea
  • JS to Put Mouse Cursor in Input Textbox or Textarea at the END of Text
  • JS to to Put Cursor at BEGINNING of Input Text or Textarea

Javascript to Put Mouse Cursor in Input Element Text or TextArea

Lets say you have a form with a form validation code that notifies the web page visitor that the form can't be submit because the user forgot to type something in one of the input fields like a textbox or textarea. An alert message to notfify them of this is great but even better is putting the mouse cursor IN that textbox or textarea so there is little confusion about what information they forgot to supply.

Note: this Javascript code is effective if there is no text in the textbox or textarea. If there is text, the textbox will get the cursor but the text will get selected as well.

Three short lines of javascript code:

javascript
1:
2:
3:
4:
var varInpBox = document.getElementById('IdOfTheTextboxOrTextArea');  // get programatic handle to the textbox or textarea
varInpBox.focus;  // set focus to the textbox/textarea
varInpBox.setActive;  // make the textbox/textarea the one that is going to receive mouse and keyboard input
varInpBox.select();  // some browsers require this line of code for it to work right

Live Demo of JS Putting Cursor in Input Element.

That's it! Many web pages I have view when searching 'how to put cursor in textbox' tell you to use one the code varInpBox.focus;. Not enough. This alone may work in some browser in certain circumstances but the second two lines of code varInpBox.setActive; and varInpBox.select(); is the finishing touch that ensures the code works properly and (almost) all the time in all browsers and devices.

Javascript to Put Cursor at a SPECIFIC Position in Input Textbox or TextArea

The above code successfully puts the cursor in the input element through sheer Javascript code but does not let you specify at what position. If there is nothing in the input element then this is not an issue or an option but if there is text, here is the code required to put the cursor in the input element at any position you wish. The code is a LITTLE more involved than the above but not much.

sel
javascript
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function setCaretPosition(ctrl, pos) {
      // For Most Web Browsers
      if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(pos, pos);
      // IE8 and below
      } else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
      }
    }

Live Demo of JS Cursor in Input at SPECIFIC POSITION.

Javascript to Put Mouse Cursor in Input Textbox or Textarea at the END of Text

The Javascript code to put the cursor in a textbox or textarea, that contains text, and put the cursor at the END of the text only requires a slight modification to the Javascript code.

javascript
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function setCaretPosEnd(ctrl) {
      var varCtlLen = ctrl.value.length;
      // For Most Web Browsers
      if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(varCtlLen, varCtlLen);
      // IE8 and below
      } else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', varCtlLen);
        range.moveStart('character', varCtlLen);
        range.select();
      }
    }

Live Demo of JS Cursor in Input at END.

Javascript to Put Cursor at BEGINNING of Input Text or TextArea

The javascript code to put the cursor in a textbox or textarea at the beginning is pretty much identical to the code above with the difference of just specifying the position variable of 0. The following code will put the cursor at the beginning, even if the Textbox or TextArea already has text in it.

javascript
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function setCaretPosStart(ctrl) {
      // For Most Web Browsers
      if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(0, 0);
      // IE8 and below
      } else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', 0);
        range.moveStart('character', 0);
        range.select();
      }
    }

Live Demo of JS Cursor in Input at BEGINNING.

CONTENT RELATED TO JAVASCRIPT: HOW TO PUT CURSOR ⌶ IN INPUT ELEMENT LIKE TEXTBOX OR TEXTAREA ▭



Javascript Scrollintoview Alternative Make Co

... es not matter what objects you employ to act as the bookmarks for the site visitor to click. What matters is the insertion of the onclick code, and pretty much any html element can except an 'onclick' ...

Html Code Example Auto Refresh Multiple Image

... mages on a single web is found in the text area below. Note: make sure to alter the html image code that is red to meet your needs, ie proper image ID, number of seconds between refreshes, etc. and ma ...

Delete Multiple All Google Voice Text Message

... es, you know the process is tedious at best. For each and ever text message you wish to delete you have to ... If you have the hidden desire to mass murder large groups of people this mind numbing pro ...

COMMENTS ON JAVASCRIPT: HOW TO PUT CURSOR ⌶ IN INPUT ELEMENT LIKE TEXTBOX OR TEXTAREA ▭

Feel free to submit your comment below. Anything that does not contribute and is just spam will automatically be deleted. Questions marked by * are required. Comments are checked by a human to make sure they are not spam/automated and are on topic and related to Javascript: Put Cursor ⌶ In Input Like Textbox ▭. 
NAME:
EMAIL:
COMMENT:
HTML OK. Allowable HTML tags include: <p></p>, <i></i>, <b></b>, <em></em>, <strong></strong>, <ul><li></li></ul>, <ol><li></li></ol>
Question ❔
Back to Top