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


Javascript: Place Mouse Cursor ⌶ In Input Like Textbox ▭

short url:

Javascript: Put Cursor ⌶ In Input Like Textbox Or Textarea ▭

Click to Play Video About Javascript፦ How To 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 ▭



Multiple Url Redirection Scripts For Javascri

... all of the code from the textbox above and place it anywhere in the web page between the <head> and </head> of the web page. If you use Wordpress you will want to install a plug in that al ...

Delete Multiple All Google Voice Text Message

... delete Google Voice text messages about twice as fast as you would be able to without this bookmarklet. Although this bookmarklet was originally designed to delete items from Google Voice text message ...

Pinterest Pin It Button On Image Hover With A

... n, any images your visitors move their mouse over will show the Pinterest Pin-It button. If you want to disable the pin button for any images, simply add the class="nopin" to that image. The small lin ...

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፦ Place Cursor ⌶ In Input Like Textbox Or Textarea ▭. 
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