Retrieving the selected value in Vue.js

Hey there! I'm currently working on retrieving the selected value of a select element. Below is the HTML code for the select:

 <select id="employee">
  <option v-bind:key="employee" v-for="employee of employees" v-bind:value="{id: employee.id}">{{ employee.name }}</option>
</select><br><br>

Here's the snippet of JavaScript where I want to log the select value to the console.

console.log(document.getElementById('employee')[0].value)

Answer №1

Initially, it's unnecessary to access the 0th element when using getElementById.

To retrieve the value, try

console.log(document.getElementById('employee').value)
.

Additionally, since you are working with Vue, consider utilizing v-model.

Note that the code snippet below is for reference purposes and has not been tested thoroughly.

<template>    
    <select id="employee" v-model="selectedEmployee">
        <option v-bind:key="employee" v-for="employee of employees" v-bind:value="{id: employee.id}">{{ employee.name }}</option>
    </select>
</template>
<script>
export default {
    data: {
        selectedEmployee: null
    },
    watch: {
        selectedEmployee: (newVal) => { console.log(newVal) }
    }
}
</script>

If you're unfamiliar with v-model, it's advised to study Vue documentation further.

Explore more on this topic by reading about Form Input Bindings in Vue

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

Is it possible to prevent a nested child node from executing its constructor until after the constructor of its parent node has been completed?

Picture a scenario where the DOM tree is structured like this: <body> <parent-component> <div> <child-component></child-component> </div> </parent-component> </body> The child component inc ...

Struggling with setting up Vue.js

Having trouble installing Vuejs and encountering an error message: What mistake am I making and how can I successfully install it? Tried reinstalling Node.js and updating npm. C:\Users\chris>npm install -g @vue/cli C:\.node_modules&bso ...

What are some strategies for blocking URL access to a controller's method in Rails 3?

My goal is to enhance my Rails 3 application by integrating Javascript/jQuery code that retrieves data from the server and updates the page based on the fetched information. I am considering implementing jQuery's $.get() method for this purpose: $.g ...

The name of the OpenLayers geometry is not preserved upon loading from WFS

While using OpenLayers 6 and GeoServer 2.16 (with PostGIS), I encountered a problem where the geometry name of loaded layers from GeoServer gets overwritten. In GeoServer, the geometry name (and column in PostGIS) is defined as geom. The XML response from ...

Vue-Loader - Using Vue Loader to Import Components from a File

I am currently in the process of converting Semantic UI CSS framework into Vue Components. I am looking to streamline this process by creating a semantic.js (or possibly semantic.vue) file that will consolidate all component imports, which can then be impo ...

"JS Kyle: Utilizing JWT for Signing and Encrypting Data

I am currently using jose for signing and encrypting JWTs, but I am facing an issue when trying to sign and then encrypt the entire JWT. When it comes to signing my JWT, I utilize the following function: const secretKey = process.env.JWT_SECRET; const key ...

Running an ESNext file from the terminal: A step-by-step guide

Recently, I delved into the world of TypeScript and developed an SDK. Here's a snippet from my .tsconfig file that outlines some of the settings: { "compilerOptions": { "moduleResolution": "node", "experimentalDecorators": true, "module ...

Is there a way for me to identify when I am "traveling" along the same path?

I want to create a toggle effect where a view is hidden if the user tries to revisit it. This can be useful for showing/hiding modal boxes. Here's the code I attempted: /* Root Instance */ const app = new Vue({ router, watch: { '$route&a ...

Is it possible to combine my Node.js/Express backend with my React.js frontend within the same directory structure?

I'm currently in the process of developing a web application that utilizes a Node.js/Express backend and a React.js frontend. At the moment, I have my Node.js code that checks login/registration information stored in a separate folder running on local ...

When utilizing useEffect in Next.js, an error may occur stating that the window is

It seems like there is a challenge with executing script code server side in next.js 13 when it needs to be executed client side. Currently, I am trying to implement the bulma calendar found at When importing the required library: import PasswordStrengthB ...

Revolutionary Approach to Efficiently Handle Multiple rows with Jquery

Greetings to everyone, I am currently in the process of developing an application that retrieves data from a database via AJAX by calling a .php file. Within this app, I have a table with 4 columns. The first two columns consist of dropdown menus, the thi ...

Is there a way to get rid of this awful hover effect on this button?

My friend and I have been developing a Chrome extension for the Roblox Devforum with some initial success. We started working on it yesterday, and already have our button placed in the header similar to extensions like Roblox+ or RoPro. However, we encoun ...

Mapping a list with sections can easily be achieved by breaking down the elements

I'm facing an issue with listing array data under sections using .map in React. I know how to use .map to list the entire array, but struggling to list each item under its respective section. Currently, I have to use .map separately for each section. ...

Receiving HTML from NodeJs instead of JSON

I am currently working on a project that involves developing an app and landing pages. While we are using NodeJs with Axios and VueJs for the app part, the landing pages are built using simple jQuery. I need to make API calls for the landing pages, but I a ...

Variable in Javascript file causing return value to be 'undefined'

I have encountered an issue with my JavaScript file. It is extracting data from a SharePoint list and displaying it on an HTML page, but one of the fields appears as 'undefined' even though I defined it initially. The problematic variable is &ap ...

Seeking a sleeker approach to composing various components with shared functions

Currently, I have a scenario where I have identical components that display data but also need to handle state manipulation and saving. There are 5 other similar components with slight differences in their functions. I am looking for a more efficient way t ...

Tips for updating the class of a button upon clicking it

I have a dilemma with my two buttons - one is green and the other has no background. I want them to change appearance when clicked, from green to one with a dashed border-bottom style. Below is the HTML code for these buttons: <div class="btns"> ...

Conceal DIV containers during the loading of the page, and reveal them only upon the selection of Radio Buttons

In my user interface, I have implemented three radio buttons labeled as "Easy," "Medium," and "Hard." Each button is assigned to a distinct Javascript function that manipulates the visibility of corresponding div elements. The main purpose behind this setu ...

Tips for transferring a function between stateful and stateless components

Looking to transfer a function from a stateful component to a stateless component, here's a snippet of the code. Below is the stateless code. const ProductsGridItem = props => { const { result } = props; const source = result._source; return ( ...

The array is devoid of any elements, despite having been efficiently mapped

I'm facing some challenges with the _.map function from underscore.js library (http://underscorejs.org). getCalories: function() { var encode = "1%20"; var calSource = "https://api.edamam.com/api/nutrition-data?app_id=#&app_key=#"; _.m ...