How can the @blur event be added to the vue-bootstrap-typeahead in Nuxt/Vue application?

I am facing an issue where I want to trigger a function upon leaving an input field. The input in question is set up using vue-bootstrap-typeahead. Upon inspecting the DOM, I found that the structure of the input element looks like this:

<div id="myElement">
  <div class="input-group input-group-sm">
    <input type="search" autocomplete="off" class="form-control">

Here is the code snippet for reference:

<vue-bootstrap-typeahead
   id="myElement"
   v-model="plateNumberFilter"
   :data="plateNumberOptions"
   size="sm"
   required
   @blur="myFunctionIsnnotTriggered"
   @hit="returnPlateNumberId()"
/>

I tried adding the id="myElement" directly to the typeahead component, but it ends up in the surrounding div instead of the actual input tag.

This leads me to three questions:

  1. How can I add an @blur event listener to the vue-bootstrap-typeahead input field?
  2. Is there a way to assign an id to the input within a vue-bootstrap-typeahead component?
  3. What is the best approach to add an eventListener to the input tag inside the vue-bootstrap-typeahead component?

You are welcome to address any or all of these questions. It would be greatly appreciated to have solutions for all three challenges.

Answer №1

The vue-typeahead-component has a limited number of available events, including only the hit and input events. This means that applying the @blur event directly to the component itself is not possible.

To listen for the blur event on the inner <input> element within the vue-bootstrap-typeahead:

  1. Add a template ref to the <vue-bootstrap-typeahead> component.
  2. Access the root DOM element of the component using vm.$el.
  3. Use Element.querySelector() to select the inner <input> element.
  4. Attach an event listener for the blur event on the <input> using
    EventTarget.addEventListener('blur', handler)
    .
<template>
  <vue-bootstrap-typeahead ref="typeahead" 1️⃣ />
</template>

<script>
export default {
  async mounted() {
    await this.$nextTick()
    this.$refs.typeahead.$el 2️⃣
        .querySelector('input') 3️⃣
        .addEventListener('blur', (e) => console.log('blur', e)) 4️⃣
  },
}
</script>

Check out the demo

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

Splitting Angular modules into separate projects with identical configurations

My Angular project currently consists of approximately 20 different modules. Whenever there is a code change in one module, the entire project needs to be deployed. I am considering breaking down my modules into separate projects for individual deployment. ...

Is it possible to pass a router-view as a prop in Vue.js?

Can a prop be passed to another component when calling it? I have components and views, where I build my views using various components. I am looking for a way to create a reusable styled component, so I thought of creating a WebsiteLayout.vue: <templa ...

Can Stencil.js be utilized to link and include multiple Stencil projects within a single Vue.js Project?

I encountered an issue while attempting to import multiple Stencil.js projects into my Vue project. Following the instructions provided for a single stencil project from this resource: However, when trying to import applyPolyFills and defineCustomElements ...

Ensuring Consistency in Array Lengths of Two Props in a Functional Component using TypeScript

Is there a way to ensure that two separate arrays passed as props to a functional component in React have the same length using TypeScript, triggering an error if they do not match? For instance, when utilizing this component within other components, it sh ...

Associating functions with events within objects

I am encountering a unique issue with jQuery and JavaScript in general. Currently, I am developing a JS file that allows me to create portlets for a client's website. I am utilizing SignalR to send updates to the users. The code snippet below is cau ...

Encountering the issue of "Unknown provider" while injecting Angular modules

After following a tutorial on organizing an Angular project, I came up with a structure where I have a ng directory containing all my controllers, services, and the routes.js file. These are then bundled together into an app.js through my configuration in ...

Typing should be positioned on either side of the declaration

When I define the type MyType, it looks like this: export type MyType = { ID: string, Name?: string }; Now, I have the option to declare a variable named myVar using three slightly different syntaxes: By placing MyType next to the variable ...

I am encountering a problem with the app.patch() function not working properly. Although the get and delete functions are functioning as expected, the patch function seems to be

I am in the process of setting up a server that can handle CRUD operations. The Movie model currently only consists of one property, which is the title. Although I can create new movies, delete existing ones, and even search for a ...

Issues with Ionic's $state.go and ui-sref functions not functioning as expected in the app

While attempting to change the view on the Ionic View app for iOS (I have not tested on the Android app), I encountered a bug that seems to have a solution, but unfortunately, it does not work for me. Bug info: Bug info 2: My Ionic information is as fol ...

Retrieve the complete HTML content of a webpage, including the values of all input fields

I'm attempting to save the entire webpage as an HTML file, including all previously entered values in input fields. I have already tried this method: $("#content").html(); However, it does not retain the values entered into input fields. $("#conten ...

Observing input value in Popover Template with Angular UI

I am currently working on a directive that utilizes the Angular Bootstrap Popover and includes an input field. Everything seems to be functioning properly, except for the fact that the watch function is not being triggered. For reference, you can view the ...

What is the best way to showcase the information stored in Firestore documents on HTML elements?

Currently in the process of designing a website to extract data from my firestore collection and exhibit each document alongside its corresponding fields. Below is the code snippet: <html> <!DOCTYPE html> <html lang="en"> <head> ...

Utilize Javascript to convert centimeters into inches

Can anyone help me with a JavaScript function that accurately converts CM to IN? I've been using the code below: function toFeet(n) { var realFeet = ((n*0.393700) / 12); var feet = Math.floor(realFeet); var inches = Math.round(10*((realFeet ...

Troubleshooting an Issue with MediaStreamRecorder in TypeScript: Dealing with

I've been working on an audio recorder that utilizes the user's PC microphone, and everything seems to be functioning correctly. However, I've encountered an error when attempting to record the audio: audioHandler.ts:45 Uncaught TypeError ...

All shadows in the THREE.js scene are perfectly aligned

I have taken a mix of examples from the three.js documentation and included the mesh.castShadow = true property on the meshes generated from the Glitch post-processing example. However, upon checking the jsfiddle link provided below, it's noticeable t ...

I encountered an error in JavaScript where the function .val() is not recognized on the selected answer, throwing

I'm trying to verify if the selected answer is correct and then add +1 to my user score. However, I'm struggling with why my code isn't functioning as expected. Can someone please point out where the bug is or if there's something else ...

Display a helpful tooltip when hovering over elements with the use of d3-tip.js

Is it possible to display a tooltip when hovering over existing SVG elements? In this example, the elements are created during data binding. However, in my case, the circles already exist in the DOM and I need to select them right after selectedElms.enter ...

backbone.js router failing to recognize URL fragments

I have set up a basic router in my code and initialized it. Issue: I encountered an unexpected behavior when visiting the URL http://localhost/backbone1/#photos/5. Despite expecting an output from console.log() in the JavaScript console, nothing appears. ...

Error encountered in Laravel API route: 'SymfonyComponentHttpKernelExceptionNotFoundHttpException' exception

As I work on creating a blog for my school project on my portfolio website, I am using VueJS and Laravel. To incorporate API routes into my project, it has become necessary. However, I encountered an issue when trying to delete a comment with a specific ID ...

Obtain the unique identifier of the chosen option using material-ui's autocomplete feature

I am currently implementing the autocomplete feature using material-ui. The data in the "customerList" displayed in the search field works fine. However, I am facing an issue with retrieving the "ID" number of the selected data. My goal is to obtain the ID ...