Categories
Vue Answers

How to get selected option on @change with Vue.js?

Spread the love

Sometimes, we want to get selected option on @change with Vue.js.

In this article, we’ll look at how to get selected option on @change with Vue.js.

How to get selected option on @change with Vue.js?

To get selected option on @change with Vue.js, we can set @change to a method that calls $event.

For instance, we write

<template>
  <select name="leaveType" @change="onChange($event)" v-model="key">
    <option value="1">Off-Day</option>
    <option value="2">On Demand Leave</option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      key: "",
    };
  },
  methods: {
    onChange(event) {
      console.log(event.target.value);
    },
  },
};
</script>

to add the onChange method that has the event parameter.

In the template, we set @change of the select element to onChange($event) to call onChange when we change the selection in the drop down.

The $event object has the native change event emitted by the select element.

So we can get the latest selected value from event.target.value.

Conclusion

To get selected option on @change with Vue.js, we can set @change to a method that calls $event.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *