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.

click-events-have-key-events: v-on:keyup.enter already triggers v-on:click event.

See original GitHub issue

Describe the bug When using a @click event, I’m prompted to add a @keyup.enter or other keyboard event. However, this frequently leads to “doubleclicks”, i.e.:

<button @click="doSomething" @keyup.enter="doSomething">

In this example, pressing Enter triggers both @keyup.enter and @click.

To Reproduce Steps to reproduce the behavior:

  1. Create element with both @click and @keyup.enter
  2. Create function, logging when function is fired by the above.
  3. Use Enter key on element.
  4. Open Developer Tools -> Console.
  5. See function has fired twice: A keyboard event and a mouse event, if you examine the event.

Expected behavior Following eslint rules shouldn’t cause strange, actual sideeffects.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Chrome
  • Version 80.0.3987.132

Additional context In my case, I detect the generated click event and return out of function with:

if(!event.sourceCapabilities) {return}

…or relevant variants where appropriate.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:12

github_iconTop GitHub Comments

11reactions
AustinGilcommented, May 23, 2020

After some chats with folks on Twiiter, apparently the updated version of this eslint plugin is https://github.com/vue-a11y/eslint-plugin-vuejs-accessibility

2reactions
TheJaredWilcurtcommented, May 21, 2020

A work around I have found is:

  <button
    @click="doThing"
    @keydown.prevent.enter=""
    @keyup.prevent.enter="doThing"
    @keyup.prevent.space="doThing"
  >
    Text
  </button>

Example: https://jsfiddle.net/frscqt0p

Explanation:

// Click event bound to method
@click="doThing"
// When you hold down enter, this prevents it from spamming the doThing function
@keydown.prevent.enter=""
// This binds the enter key to doThing, but prevents it from triggering the click event
@keyup.prevent.enter="doThing"
// This binds the space key to doThing, but prevents it from triggering the click event
@keyup.prevent.space="doThing"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jquery: how to trigger click event on pressing enter key
I have used trigger() to execute click and bind it on the keyup event insted of keydown because click event comprises of two...
Read more >
Click events are triggered with 'enter' keyPress ... - GitHub
But this issue is not about the button. It's about how a keypress event is magically converted to a click event on the...
Read more >
How To Trigger Button Click on Enter - W3Schools
Trigger a Button Click on Enter. Press the "Enter" key inside the input field to trigger the button: Button. Example.
Read more >
JavaScript | Trigger a button on ENTER key - GeeksforGeeks
To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This...
Read more >
Event Handling - Vue.js
Inline handlers: Inline JavaScript to be executed when the event is triggered (similar to the native onclick attribute). Method handlers: A property name...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found