question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autofocus Text puts cursor at end instead of beginning

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? In React 16, a text input with a value and autofocus will set the cursor at the end of the input. <input autoFocus={true} type="text" defaultValue="4444" />

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

See the React 16 repro here: https://codepen.io/matthewg0/pen/XymjYo

What is the expected behavior? In React 15, and in plain HTML, the behavior is that the cursor is placed at the start of the input. React 15: https://codepen.io/matthewg0/pen/NEGRzv Plain HTML: https://codepen.io/matthewg0/pen/wQKoap

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? Reproduces as of React 16. Worked as expected in React 15.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Matthew-Goldbergcommented, Jan 27, 2020

OK the jsdom issue is now fixed: https://github.com/jsdom/jsdom/pull/2799 When that is released we can upgrade the jsdom version to pick up the fix, and re-implement the test which should now be useful.

1reaction
Matthew-Goldbergcommented, Nov 9, 2018

@mmarkelov I think this will be hard to write a test, it seems jsdom is showing that the selectionStart is 0 even though in the browser the same code shows selectionStart is the end/length of the text value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autofocus With Cursor On End Of Textbox - Stack Overflow
I want to put autofocus on a html textbox such that the cursor points to the end of some text already present in...
Read more >
Move the Cursor to the End of an Input field using JS
To move the cursor to the end of an input field, use the `setSelectionRange()` method to set the current text selection position to...
Read more >
How to place cursor position at end of text in text input field ...
In this article, we are going to learn about how to place the cursor at end of the text in a text input...
Read more >
Put cursor at the end of an input - HTML DOM
Assume that we have a text field representing the full name of an user. There is also a Edit button for updating the...
Read more >
How to Set cursor position while focus on the input textbox in ...
The selectionStart and selectionEnd set to 0 instead of the input element value's length, when we focus on a MaskedTextBox control filled with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found