Pressing the Enter key on a <li> element using Vue3

Here is the code snippet:

<li v-for="(dog, index) in dogs" :key="index" class=" hover:bg-gray-50" :class="{ 'bg-red-50': index === focus }" @keyup.enter="goToSlug(dog)"> .... </li>

The focus is handled correctly, but I'm experiencing an issue with running the method goToSlug() when the enter key is pressed. The method does not seem to fire as expected.

Answer №1

Key inputs are only recognized on elements that are currently in focus.

To enable a non-focusable element like an <li> tag to receive focus, you must include an additional attribute called tabindex='1' (the value '1' is arbitrary but necessary; for more information, visit here).

For example:

<li 
  v-for="(dog, index) in dogs" 
  tabindex="1" 
  :key="index" 
  class=" hover:bg-gray-50" 
  :class="{ 'bg-red-50': index === focus }" 
  @keyup.enter="goToSlug(dog)"
> .... 
</li>

By tabbing through the items and pressing enter on your desired one, key presses can be detected on this element or its siblings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to integrate AngularJS with jQuery?

Lately, I've been incorporating AngularJS into my project. In the past, I've written various prototypes and standalone JavaScript functions, such as (Node.js + socket.io functionality). However, I am currently facing some challenges when it come ...

Using Vue Router's router-link within a Google Maps info window

I have everything working perfectly, but I would like to replace the <a> tag with the <router-link> tag. This is because the <a> tag reloads the page, unlike the <router-link>, which intercepts the click event and prevents the page ...

Does Ajax XMLHttpRequest trigger the execution of servlet doFilter?

I've implemented an AJAX call from a JSP page as shown below: <head> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, ...

The React component fails to update upon a change in state

When dispatching an object to an array, I receive a notification that it was successfully sent, but the length of this array does not change in my Header component. This means that: store.getState().Basket.length is not re-rendering. The value only update ...

Web Components follow relative paths starting from the root directory

As I work on creating a web component using native implementation, I have encountered an issue with the links to images in its HTML template. These links only function correctly if they are absolute or relative to the main document, making the component no ...

Navigating the storing and organizing of user data through Firebase's simplistic login feature for Facebook

As I work on my AngularJS and Firebase-powered website project, I aim to leverage Facebook login for seamless user connectivity. While Firebase's simple login feature promises an easier authentication process, I face the challenge of effectively acces ...

The JavaScript function exclusively reveals the final element, which is Three.js

I'm currently working on a fence generator function using Three.js, but for some reason, my function is only returning the last fence created. It's really puzzling to me... function createFence(nb){ var i; var position = -5; var loadingMan ...

Performing a JavaScript AJAX request to send a complex object containing an array of other complex objects within it

My issue arises from encountering an empty array of objects at the backend. To illustrate, I have established two classes at the backend... public class ScoreModel { public string Subject { get; set; } public float Score { get; set; } ...

Struggling with developing a straightforward application with Angular-Material

My goal is to develop an application that utilizes the Angular Material navigation bar, as showcased in this example. Being relatively new to AngularJS, I'm facing an issue where my app loads but only displays a blank page. Below is the code snippet ...

Is it a Mozilla Firefox glitch or something else?

After writing the code, I noticed a bug in Firefox and a small bug in Chrome. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

The sound in ThreeJS continues to play even after navigating to a different page using React Link and the UseEffect hook

I am currently working on a project involving ThreeJS and sound integration. Within my scene, I have various objects with audio that are retrieved from a Firebase database. Utilizing Mobx as a state manager, there is a button present to navigate to a diffe ...

How to use jQuery to delete specific table rows while keeping the first row safe from removal

Currently, I have a jQuery script that successfully removes table rows when a button is clicked. However, I want to prevent the button from removing the first row of the table. Is there a way to achieve this? $("#remove").click(function(event) { ...

Knowing when to use Provide/Inject and when to use config.globalProperties in Vue.js is essential

Working on a small VueJs SPA with my first experience with VueJs 3. Previously, you could easily add helpers and classes to the Vue instance using prototype. However, VueJs 3 has eliminated this capability and introduced two new options: provide / inject ...

Update all field values in Redux-form version 6.0 and above

I am attempting to update several values in redux-form. They are stored in a single object, and I want to replace the current redux-form state values with those from my object. One method I have tried involves using this.props.reset() followed by multipl ...

The CSS styles are failing to be applied to the submit button once it has been

When I have a form with a submit button that triggers a form submission on a PHP page, I want to have an onclick event on the submit button that changes its opacity using JavaScript. The issue I am facing is that after clicking the submit button, the opaci ...

What occurs when a JavaScript variable is assigned a nonexistent variable?

When it comes to setting a javascript variable, I'm assigning it with a value as var newline = $("#newline").val(); It's important to note that the $("#newline").val() may or may not exist. Sometimes, the #newline may not be present in the DOM, ...

Tips for navigating to a different route during the ngOnInit lifecycle event

How can I automatically redirect users to a specific page when they paste a URL directly into the browser? I would like users to be directed to the /en/sell page when they enter this URL: http://localhost:3000/en/sell/confirmation Below is the code I am ...

Interactive front end design for decision trees

On the front end, I aim to enable users to influence the outcome of a Decision Tree based on their selections. For my Django-React App, I have adopted the style and tree example from the codeplayer. You can find it here: I am tasked with creating an unor ...

MeteorJS: Verification of User Email addresses

After sending an email verification to a user, how can I ensure they actually verify their email after clicking on the link sent to their address? I'm aware of this function Accounts.onEmailVerificationLink but I'm unsure of how to implement i ...

Creating interactive web pages by dynamically adding div elements using JavaScript

I am attempting to incorporate HTML divs (with textfields) through a button click event. The structure of my form is as follows: When the add size button is clicked, it adds one row containing Price and size. Upon clicking the add More item button, I wish ...