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


Javascript፦ How To Put Cursor ⌶ In Input Like Textbox ▭

short url:
  • 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 ▭



How To Put Mouse Cursor Inside Input Text Tex

... mo of JS Cursor in Input at END. 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 p ...

Delete Multiple All Google Voice Text Message

... (bookmarklet is basically the same as a web browser extension except it works in ALL web browsers, not just one). This bookmarklet is called the Fast Google Voice Text Message Delete and is 100% free ...

How To Auto Refresh Image On Web Page

... to auto refresh an image at set intervals: Because PHP is a server sided and not client side programming language, you can't directly auto refresh an image on a web page using PHP. It can be accomplis ...

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 Script: How To Place Mouse Cursor ⌶ In Input ▭. 
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