Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to automatically set the cursor to an input tag

I have an input tag and what im trying to do is listen for the / key press and automatically set the cursor to the input tag to start typing on it. Google already has this and will automatically go to the search bar when you press the / key. How can I achive this. Any help is appreciated. Thanks in advance.

document.onkeypress = function(evt) {
  evt = evt || window.event;
  var charCode = evt.keyCode || evt.which;
  var charStr = String.fromCharCode(charCode);
  if (charStr == '/') {
    //set cursor to input bar
  }
};
#inputBar {
  position: absolute;
  top: 2%;
  left: 3%;
}
<input id="inputBar" />

UPDATE: as suggested the focus method will do it but the / charater will also get registerd on the input bar. How should i handle this? Sholud i make a separate function that removes the / char if detected within < 10ms of the / keypress. Or any other better ideas?

document.onkeypress = function(evt) {
  evt = evt || window.event;
  var charCode = evt.keyCode || evt.which;
  var charStr = String.fromCharCode(charCode);
  if (charStr == '/') {
    //set cursor to input bar
    document.getElementById("inputBar").focus();
  }
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use a combination of preventDefault to stop what the event was going to do, and focus, to focus on the input. You also need a way to identify the the body element was clicked for which you need nodeName so you can still use the input element.

Your code (note: which uses the now-deprecated keycode listener) updated to use these methods:

var input = document.getElementById('inputBar');

document.onkeypress = function(evt) {
  evt = evt || window.event;
  var nodeName = evt.target.nodeName;
  var charCode = evt.keyCode || evt.which;
  var charStr = String.fromCharCode(charCode);
  if (nodeName === 'BODY' && charStr == '/') {
    evt.preventDefault();
    input.focus();
  }
};
<input id="inputBar" />

Some more modern code that uses code (thanks for Meron Ogbai for pointing out that keypress is also deprecated).

const input = document.querySelector('#inputBar');

document.addEventListener('keydown', handleKey, false);

function handleKey(e) {
  const { code } = e;
  const { nodeName } = e.target;
  if (nodeName === 'BODY') {
    e.preventDefault();
    if (code === 'Slash') input.focus();
  }
}
<input id="inputBar" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading