New issue
Advanced search Search tips

Issue 729072 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Jun 2017
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Linux
Pri: 2
Type: Bug



Sign in to add a comment

Chrome processes keypress event twice when forcing element focus on keypress

Reported by frederik...@gmail.com, Jun 2 2017

Issue description

UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Steps to reproduce the problem:
1. Create a document containing nothing but the following markup:
```
<form action="/">
  <input type="search" name="q">
</form>
```
2. Run the following snippet of JavaScript:
```
document.body.addEventListener('keypress', function(e) {
  const inputElem = document.querySelector('input');
	const previousValue = inputElem.value;
  inputElem.value = previousValue + e.key;
  inputElem.focus();
});
```
3. Without focussing the input, press the "a" button on your keyboard.

What is the expected behavior?
The input field should now be in a focused state and contain the letter "a".

What went wrong?
The input field contains the letter "a" twice.

Did this work before? N/A 

Chrome version: 58.0.3029.110  Channel: stable
OS Version: 
Flash Version: Shockwave Flash 25.0 r0

Online fiddle to reproduce the issue: https://jsfiddle.net/s1Lztfne/

Works as expected in other major browsers.
 
Status: WontFix (was: Unconfirmed)
Fails in FireFox as well on Ubuntu as well.

This code is wrong. If you want to handle the keypress in a javascript listener you should preventDefault the action. Calling e.preventDefault(); inside the event listener fixes the issue of the double "a".

As you see in the code that the keypress listener executes before the text is inserted into the form control but you update the value to contain that value as well. So by the time the user agent processes the keypress (which isn't prevent defaulted in the example) the input value is "a" so then it appends the other "a" to it to result in "aa".

Cc: dtapu...@chromium.org
Components: -Blink Blink>Input
I'm aware of `preventDefault` and this is how I deal with it in my actual code, yet against your claims this still behaves differently in Firefox for me (both on my Linux machine as well as on BrowserStack) so I thought I'd raise this issue. Anyways thanks for looking at this.

Sign in to add a comment