Executing Function Upon Clicking an Option in Datalist

I'm facing an issue with datalist in my VueJS search input. Why isn't my function getData(), which is triggered by both @click and @keypress.enter, being called when I select an option from the datalist using mouse or enter key?

Below is the code snippet:

template:

<input
            list="data"
            v-model.trim="searchBy"
            @keyup.enter="getAllDatas"
            type="text"
            placeholder="Find more data..."
          />
          <br />
          <datalist id="data">
              <option v-for="r in repos.items" 
                      :key="r.id" 
                      :value="r.name"
                      @click="getData(r.id)"
                      @keypress.enter="getData(r.id)"  />
          </datalist>

script:

 methods: {
    getAllDatas() {
      fetch(`someURL`)
        .then(res => res.json())
        .then(
          res => {
            this.data = res
          }
        )
    },
    ...mapActions(["fetchData"]),
    async getData() {
       await this.fetchData();
  },

I'm puzzled as to what might be causing this problem. Any insights would be greatly appreciated.

Answer №1

The <datalist> element does not come with its own set of events, so you will need to utilize the <input> element instead. The input's keydown listener will be triggered by both mouse clicks and keyboard actions.

<input
  list="data"
  v-model.trim="searchBy"
  @keydown="getData"
  @keyup.enter="getAllDatas"
  type="text"
  placeholder="Search for more data..."
/>

If this event handling setup doesn't suit your needs, consider using a <select> element or creating a custom control instead.

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

Tapping on the link does not show the image on iOS devices

I stumbled upon a clever workaround since JavaScript is disabled in our application. This hack works perfectly on Android, but unfortunately not on iOS. Simply click on "Open" to reveal your image and change the link to "Close"; clicking on "Close" will h ...

Discovering the names of files in a directory with Angular

Looking for a solution in Angular JS within a ModX app to retrieve file names from the gallery package every time it is updated. Is there a way to achieve this using Angular? I've been searching for Javascript solutions to this issue, but most of the ...

Setting non-reactive data in Vue 2 components: A step-by-step guide

I have an array of categories that is loaded once in the created hook and remains static thereafter. I am rendering these values in a component template. <template> <ul> <li v-for="item in myArray">{{ item }}</li& ...

Angular detects when a user scrolls on a webpage

I have developed a straightforward navigation system using Angular. The main controller is responsible for generating the menu. <nav class="{{active}}" ng-click= ""> <a href="#a" class="home" ng-click= "active='home'">Home< ...

Making changes to the retrieved properties using Apollo GraphQL within a Vue.js environment

I'm trying to utilize the Moment javascript library to format a date that I retrieve through Apollo-GraphQL. My setup involves VueJS Apollo on the client side for executing graphQL queries like this: import { ALL_EVENTS } from '../constants/grap ...

What is the best way to generate multiple progress bars by leveraging a for loop?

Exploring the code snippet below, I've been creating a progress bar that reacts to specific data input in array form. However, the issue I've encountered is that the code only generates a single progress bar. How can I incorporate this into my f ...

Understanding the Process of Accessing Array Values in Immutable.js

I'm feeling a little puzzled and struggling to figure this out. Let's say I have the following code: const AnObj = Immutable.Map({ a : "a", b : Immutable.List.of( a, b, c, Immutable.Map({ a : "a" }) ) }); When working with Immu ...

What could be causing that unusual behavior within the quasar framework's select CSS?

I am facing an issue with a select control that needs to be aligned on the right side of the screen. Despite using HTML and CSS to achieve this, there is strange behavior when I initially click on the control. It breaks the options layout, but as I navigat ...

Retrieve data from a variable that is located within a function that is also

<script> const tally ={ total: 0, increase: function(){ total++; console.log(total); } } const selectBtn = document.getElementsByTagName('button& ...

JavaScript encountered a problem when trying to call the inline function

I created a function called controls that adds HTML elements dynamically into the specified div. function controls() { var block = $('#controls'); block.html(''); block.append('<button type="button" onClick="f ...

"Exploring the process of implementing a fixed method POST in Angular 5

When developing an application for Portal, I encountered an issue where a newly created role is not displayed without refreshing the browser. How can I ensure that the added element is directly displayed in the table without needing to refresh the browser? ...

Creating an infinite loop using Jquery's append and setTimeout functions

I'm having trouble displaying my JSON data in a table and refreshing it periodically to check for new entries. Unfortunately, I seem to have gotten stuck in an infinite loop where the setTimeOut function keeps adding old entries. Can anyone help me tr ...

Verify whether the type of the emitted variable aligns with the specified custom type

Currently, I am in the process of testing Vue 3 components using jest. My main objective is to receive an emit when a button is clicked and then verify if the emitted object corresponds to a custom type that I have defined in a separate file. Below is an e ...

Utilize the ng-file-upload library to easily upload files to MongoDB's GridFS

I need to upload a file larger than 16MB to my Mongo database. I am using the ng-file-upload module on the front end (https://github.com/danialfarid/ng-file-upload). On the back-end, I am utilizing connect-busboy and gridfs-stream modules. However, I enc ...

I'm looking for a way to seamlessly replace images with a smooth opacity transition in Vue. Any suggestions on

After successfully creating a code that switches the current image after 3 seconds, I noticed that the new image appears abruptly without a smooth opacity effect. This lack of transition looks unappealing and I am eager to rectify it using VueJS. However, ...

Guide for transforming a list of items into nested structures based on a key comparison

Objective: The goal is to organize the list of objects into a nested object structure based on their parent values. Here is the json data provided: "data": [ { "id": "coding-825x500", "source": { "information": { "fileid ...

What is the process for configuring the initialRange in Vue-rangedate-picker-winslow?

Struggling with the configuration of the Initial Range prop while using the Vue Rangedate Picker. This prop determines the initial range displayed by the component before any user selection. I have successfully set up other props such as "caption" and "pr ...

Removing background color and opacity with JavaScript

Is there a way to eliminate the background-color and opacity attributes using JavaScript exclusively (without relying on jQuery)? I attempted the following: document.getElementById('darkOverlay').style.removeProperty("background-color"); docume ...

Using JavaScript to ensure that a background image is only displayed once in an HTML document

I am currently working with two cells, where I have set background images for these cells using Javascript from an array of images. The images available are: image1= 150*150 referred to as image1 once image2=150*150 referred to as image2 once However, ...

A more efficient method for importing numerous 'export' statements using ES6 or TypeScript

Currently, I am utilizing D3.js V4 with the module and my goal is to import multiple modules into a singular namespace. The code snippet provided below showcases my current approach, but I am curious if there is a more efficient method available. const d3 ...