Obtain the event object within the Vuex v-model setter method

I am working with a select element that interacts with my Vuex store:

<select v-model:value="leadTrackNumber"  >
  <option v-for="(track, index) in tracks ">{{ index }}</option>
</select>

Here is the corresponding method:

leadTrackNumber: {
  get(){
    // process some data
    return leadTrackNumber
  },
  set(value){
    this.$store.commit('updateLeadTrack', value )
  },
},

I want to use something like

v-model:value="leadTrackNumber(value, $event)"
and then receive it in the method as set(value, event) (although this code does not actually work). Is there a way to achieve this?

The goal is to utilize the received event argument to blur the select element. (By using event.target.blur())

I am seeking an answer to this question out of curiosity, but I am also open to alternative methods. (For example, I can call a blur function on change like this: @change="blurFunction($event)", but that only works when the value changes - I want it to blur regardless.)

Answer №1

It seems that by default, when you choose an option, the select element receives an outline color indicating it is focused.

However, if you prefer not to have this focus effect and instead want the focus to be removed when an option is selected, there are a few ways to achieve this. One way is to toggle a CSS class to add or remove the outline color, but I will show you how to do it using JavaScript and Vue.js.

<div id="app">
  <select v-model="selected_item" @click="option_selected"> <!-- Call the option_selected method when an option is selected -->
    <option :value="null">Select item</option>
    <option v-for="todo in todos" :key="todo.id" :value="todo.text">
      {{todo.text}}
    </option>
  </select>
</div>

Here is the Vue.js code:

new Vue({
  el: "#app",
  data: {
    selected_item: null,
    todos: [
      { text: "Learn JavaScript", id: 1 },
      { text: "Learn Vue", id: 2 },
      { text: "Try JSFiddle", id: 3 },
      { text: "Create something amazing", id: 4 }
    ]
  },
  methods: {
    option_selected(event) {
        event.target.blur()
    }
  }
})

Feel free to check out this JSFiddle example too!

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

Error: Cannot access 'addEventListener' property of null in Chrome Extension due to TypeError

I've been working on developing a chrome extension that autofills input forms in web pages. However, I encountered an error that says "cannot read properties of null." This issue arises when I try to add an event listener to a button in my code. The f ...

A guide on implementing AJAX redirection in MVC

I currently have a button that, when clicked, takes input values and redirects to another page in JavaScript using the following code window.location = "Action Param1=value1&Param2=Value2". However, I am looking to avoid using query strings for this me ...

Navigating the browser view across an extensive HTML page without relying on scrollbars

I have a large HTML page (approximately 7000x5000) and I need to be able to move the user's view around with JavaScript. Disabling the ability for the user to scroll by hiding the browser scrollbars using overflow:hidden on the HTML element is easy. ...

Angular's minimum date validation is not accurate for dates prior to the year 1901

Any assistance or clarification on this matter would be greatly appreciated. It appears that there may be an issue with my implementation, as otherwise it seems like a significant bug within Angular. Setup Create a form with a minimum date of 0001-01-01 ...

Tips for putting distance between the digits in a number

Is there a way in Vue to format an input field that displays "10000" as "10 000" when typed by the user? ...

converting nested object structures in typescript

I'm trying to flatten a nested object in my Loopback and Typescript controller Here's the structure of my model : export class SampleModel { id: number; code: number; guide?: string; gradeData?: string; } Take a look at this example obj ...

Is it considered inefficient to import every single one of my web components using separate <script> tags?

Currently, I am in the process of developing a website with Express + EJS where server-side rendering is crucial. To achieve this, I am utilizing web components without shadow dom. Each page type (home, post, page) has its own EJS view. For instance, when ...

JavaScript will continue to process the submit to the server even after validation has been completed

My current challenge involves implementing form validation using JavaScript. The goal is to prevent the form from being sent to the server if any errors are found. I have made sure that my JavaScript function returns false in case of an error, like so: ...

Using Vue to Pass Selected Value from Select Component to Another Component

Here is the template I am working with: <div class="container"> <div class="row"> <div class="col-lg-5 filter big"> <select v-model="stc" @change="getData" name= ...

Ways to verify if a user is authenticated without relying on request.session

I am currently developing a web application using Express, Docker, and following a Three-layered architecture. In my app, I store user login information in a session and have blogposts as a key resource. To retrieve the blogpostId from the database in the ...

Calculating the sum in a VueJS pivot table

Currently, I am delving into VueJS and working on migrating a basic application from Laravel with the blade template engine. The backend remains unchanged, consisting of a straightforward RESTful API with 3 tables: Books, Places, and a pivot table named B ...

Tool designed to analyze the timing of sub requests and methods in Node for benchmarking purposes

For my benchmarking and load testing needs, I initially utilized tools such as Apache Bench, Siege, and benchmark.js. However, these tools only provided me with the overall result or time taken from start to finish of the test. I am now seeking a tool or l ...

When viewing my project on GitHub pages, the images within the card are not appearing

After running my project page link at https://nayaksofia.github.io/RestaurantReviewTest1/, I've noticed that the images inside the card are not displaying. However, when I run the same project on my local server localhost:8000, the images appear just ...

Disabling the scrollbar in Selenium screenshots

When using Chromedriver to capture screenshots of webpages, my code effectively does the job. However, I am now facing an issue with removing the unsightly scrollbars from the image. Is it feasible to inject CSS into the webpage in order to achieve this? W ...

Filtering data in a table using Vue.js on the client side

I am facing an issue with filtering a table containing student details retrieved from a database using v-for. I am attempting to filter the table based on a specific field value. To begin with, I have three input fields located above the table, each bound ...

Is it possible that binding a ref is not functional in vue.js?

Whenever I use v-bind to bind an element reference with :ref="testThis", it appears to stop functioning. Take a look at this working version: <template> <div> <q-btn round big color='red' @click="IconClick"> ...

There appears to be an issue with Javascript's ability to handle JSON API requests

I'm currently working on a webpage that utilizes the openweathermap API to showcase the user's city and local temperature. Unfortunately, I'm encountering an issue where the JSON API is not being processed and nothing is happening. Despite r ...

Steps for creating a TypeScript project for exporting purposes

Forgive me for my lack of experience in the js ecosystem. Transitioning from static languages to typescript has been a positive change, though I still find myself struggling to grasp the packaging/module system, especially when coupled with typescript defi ...

Video demo: Issue with Bootstrap navigation bar expansion being truncated

When developing my React app, I installed the latest Bootstrap using npm by running: npm install bootstrap followed by: npm install bootstrap jquery and then: npm install jquery popper.js Initially, my Navbar functioned perfectly when the window was wid ...

Can you explain the distinction between using router.METHOD() versus router.route() in Express?

There are two different ways I've come across of writing this code. router.get(path, callback) and router.route(path).get(callback) Based on the surrounding code, they seem to have the same functionality. The documentation for these methods can be ...