118
document.getElementById(frmObj.id).value="";
document.getElementById(frmObj.id).autofocus;
document.getElementById("errorMsg").innerHTML = "Only numeric value is allowed";

In the above code the value of the form object is perfectly setting to "" but there is no cursor in the text box. I want a cursor to be there. focus() only focuses that input box but does not actually set the cursor.

4
  • 2
    document.getElementById(fieldID).focus(); should put the blinking cursor in the form field unless you have something else that grabs the focus on the page. The statement with .autofocus you have now does nothing Commented Jun 5, 2012 at 9:18
  • 4
    .focus() does set cursor. This is the whole meaning of the focus action. Commented Jun 5, 2012 at 9:20
  • 1
    if focus doesn't work, i would try click() Commented Jun 5, 2012 at 9:21
  • 4
    Future readers: also make sure that the element you are targeting has been added to the DOM. I was creating elements in the script and calling focus() on them before I appended them. Switching the order fixed the issue for me. Commented Jun 24, 2017 at 16:15

8 Answers 8

144

In JavaScript first focus on the control and then select the control to display the cursor on texbox...

document.getElementById(frmObj.id).focus();
document.getElementById(frmObj.id).select();

or by using jQuery

$("#textboxID").focus();
Sign up to request clarification or add additional context in comments.

5 Comments

There is no need for the select. There is something else going on with his code. Possibly an embedded PDF or similar
i want that cursor should be there in textbox. i should not click in textbox to enter value.
In fact, in my case, the select made the code NOT work. You just want the focus();
Worked for Me with each, With Focus Cursor is placed at end of input and you can start writing from the end with other characters preserved. With select the value inside is also selected.
focus doesn't make any sense (
74

I realize that this is quite and old question, but I have a 'stupid' solution to a similar problem which maybe could help someone.

I experienced the same problem with a text box which shown as selected (by the Focus method in JQuery), but did not take the cursor in.

The fact is that I had the Debugger window open to see what is happening and THAT window was stealing the focus. The solution is banally simple: just close the Debugger and everything is fine...1 hour spent in testing!

4 Comments

I was trying to make a <span> element focusable and this ended up being the issue (glad I scrolled passed the first accepted answer). I also found that there are html elements that are not focusable by default (seems to be browser-specific). The <span> element is one of those elements which is why Chrome (Version 57) gave focus to the developer tools window.
This comments saved me minutes rather than hours!
Unfortunately closing the developer console does not help. The .focus() is called correctly, but the element does not get the desired focus. Most likely something changed in newer browsers. Anyways, thanks for sharing your idea!
@sirko I am in same situation , when I type something in input box and with mouse select the whole value and by using backspace delete the value. While deleting mouse hover is on pdf document. .focus() method is taking to input element to focus programmatically but cursor is not blinking. I think this is something with browser . I have pdf document and input fields as siblings where I have left side pdf and right side input fields . I am trying to select the value in input field with mouse and when mouse hover enters the pdf cursor blink is not coming . Any workaround much appreciated
8

Sometimes you do get focus but no cursor in a text field. In this case you would do this:

document.getElementById(frmObj.id).select();

3 Comments

Yes, this is correct sometime when there is a transition .focus() will not work you will have to use $("element")[0].select() in order to get the cursor
or el.setSelectionRange(el.value.length, el.value.length); to get the cursor at the end of the line.
Note : setSelectionRange would NOT work on <input type="number"/>
8

This way sets the focus and cursor to the end of your input:

div.getElementsByTagName("input")[0].focus();
div.getElementsByTagName("input")[0].setSelectionRange(div.getElementsByTagName("input")[0].value.length,div.getElementsByTagName("input")[0].value.length,"forward");

1 Comment

Work perfectly, but I think the code should be like this document.getElementsByTagName("input")[0].focus();
6

One of the things that can bite you is if you are using .onmousedown as your user interaction; when you do that, and then an attempt is immediately made to select a field, it won't happen, because the mouse is being held down on something else. So change to .onmouseup and viola, now focus() works, because the mouse is in an un-clicked state when the attempt to change focus is made.

1 Comment

I know this is an old question, but this answer definitely deserves recognition. Couldn't find anyone else that mentioned this. Thank you
2

Inside the input tag you can add autoFocus={true} for anyone using jsx/react.

<input
 type="email"
 name="email"
 onChange={e => setEmail(e.target.value)}
 value={email}
 placeholder={"Email..."}
 autoFocus={true}
/>

1 Comment

This is clearly not javascript ;-)
1

You have not provided enough code to help You likely submit the form and reload the page OR you have an object on the page like an embedded PDF that steals the focus.

Here is the canonical plain javascript method of validating a form It can be improved with onubtrusive JS which will remove the inline script, but this is the starting point:

function validate(formObj) {
  document.getElementById("errorMsg").innerHTML = "";
  var quantity = formObj.quantity;
  if (isNaN(quantity)) {
    quantity.value = "";
    quantity.focus();
    document.getElementById("errorMsg").innerHTML = "Only numeric value is allowed";
    return false;
  }
  return true; // allow submit
}
#errorMsg { color:red }
<form onsubmit="return validate(this)">
  <input type="text" name="quantity" value="" />
  <input type="submit" />
</form>
<span id="errorMsg"></span>

Comments

0

In my experience

document.getElementById(frmObj.id).focus();

is good on a browser running on a PC. But on mobile if you want the keyboard to show up so the user can input directly then you also need:

document.getElementById(frmObj.id).select();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.