179

I have a combobox and want to do something different based on the selected combobox. I use a separate vue.html and TypeScript file. Here's my code:

<select name="LeaveType" @change="onChange()" class="form-control">
  <option value="1">Annual Leave/ Off-Day</option>
  <option value="2">On Demand Leave</option>
</select>

Here's my TypeScript file:

onChange(value) {
  console.log(value);
}

How do I get the selected option value in my TypeScript function?

kissu
  • 40,416
  • 14
  • 65
  • 133
hphp
  • 2,142
  • 2
  • 13
  • 26

6 Answers6

306

Use v-model to bind the value of selected option's value. Here is an example.

<select name="LeaveType" @change="onChange($event)" class="form-control" v-model="key">
   <option value="1">Annual Leave/ Off-Day</option>
   <option value="2">On Demand Leave</option>
</select>
<script>
var vm = new Vue({
    data: {
        key: ""
    },
    methods: {
        onChange(event) {
            console.log(event.target.value)
        }
    }
}
</script>

More reference can been seen from here.

tony19
  • 125,647
  • 18
  • 229
  • 307
ramwin
  • 5,803
  • 3
  • 27
  • 29
  • 1
    it's getting error "key is not defined". do i have to define v-model first? if yes, how? – hphp Jun 22 '18 at 07:28
  • 1
    Yes, you can set any name to replace the key. But make sure that this is a property of data. – ramwin Jun 22 '18 at 08:28
  • I still, dont get it.. i know i can rename it to any name, but i getting a warning "[Vue warn]: Property or method "selected" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property" – hphp Jun 22 '18 at 08:59
  • 1
    Maybe there is a typo in your script because there is no existance of word 'selected' in the script. Please check it and refer to the link in the answer. I add a demo example in it. – ramwin Jun 22 '18 at 09:45
  • "selected" is my model name. if i rewrite your code, my warning will become "key" is not defined – hphp Jun 25 '18 at 03:13
  • 1
    "key" has been defined in the data. Can you copy your code to [jsfiddle](https://jsfiddle.net/) or github? – ramwin Jun 26 '18 at 02:51
  • 1
    The link you add didn't work. An error `Uncaught SyntaxError: Unexpected token export` occurs. Pleay don't use es6. – ramwin Jun 28 '18 at 02:06
  • it's solved. just initialize the value when it's declared. Thanks – hphp Sep 18 '18 at 09:34
  • since the tag says asp.net-mvc, remember that @ invokes razor syntax in a view, so imo its preferable to use v-on – Terje Solem Jan 14 '19 at 20:11
  • It's also possible to use `this.key` instead of `event.target.value` inside `onChange` method. – Mustafa Ehsan Alokozay Oct 19 '19 at 09:34
  • Why use @change. This is what watch is made for. Here is the modified [example](https://jsfiddle.net/csp8wy6n/) of the example in the answer. – E. Hekkert Nov 24 '21 at 20:00
52

@ is a shortcut option for v-on. Use @ only when you want to execute some Vue methods. As you are not executing Vue methods, instead you are calling javascript function, you need to use onchange attribute to call javascript function

<select name="LeaveType" onchange="onChange(this.value)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

function onChange(value) {
  console.log(value);
}

If you want to call Vue methods, do it like this-

<select name="LeaveType" @change="onChange($event)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

new Vue({
  ...
  ...
  methods:{
    onChange:function(event){
       console.log(event.target.value);
    }
  }
})

You can use v-model data attribute on the select element to bind the value.

<select v-model="selectedValue" name="LeaveType" onchange="onChange(this.value)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

new Vue({
    data:{
        selectedValue : 1, // First option will be selected by default
    },
    ...
    ...
    methods:{
        onChange:function(event){
            console.log(this.selectedValue);
        }
    }
})

Hope this Helps :-)

santanu bera
  • 1,281
  • 10
  • 16
  • I get the following error when I execute second one in vuejs: Uncaught TypeError: Cannot read property 'apply' of undefined – TheDevWay Nov 05 '18 at 11:14
  • I haven't used "apply" anywhere in the Code. Could you please share the code in which you are getting the error? – santanu bera Jan 22 '19 at 12:41
32

The changed value will be in event.target.value

const app = new Vue({
  el: "#app",
  data: function() {
    return {
      message: "Vue"
    }
  },
  methods: {
    onChange(event) {
      console.log(event.target.value);
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id="app">
  <select name="LeaveType" @change="onChange" class="form-control">
   <option value="1">Annual Leave/ Off-Day</option>
   <option value="2">On Demand Leave</option>
</select>
</div>
Agney
  • 18,522
  • 7
  • 57
  • 75
20

You can also use v-model for the rescue

<template>
    <select name="LeaveType" v-model="leaveType" @change="onChange()" class="form-control">
         <option value="1">Annual Leave/ Off-Day</option>
         <option value="2">On Demand Leave</option>
    </select>
</template>

<script>
export default {
    data() {
        return {
            leaveType: '',
        }
    },

    methods: {
        onChange() {
            console.log('The new value is: ', this.leaveType)
        }
    }
}
</script>
tony19
  • 125,647
  • 18
  • 229
  • 307
Merhawi Fissehaye
  • 2,482
  • 2
  • 25
  • 39
6

onChangeMethod --> method of your choice, it can be anything related to your program.

<template>
<select @change="onChangeMethod($event)">                                            
  <option value="test">Test</option>
  <option value="test2">Test2</option>
</select>
</template>

<script>
 data(){
   return{
    ...
     
   }
 },
 methods:{
     onChangeMethod(event)
     {
         console.log(event.target.value);
     }
 },
 created(){
   ...
 }   
</script>                                                                          
Rxhack com
  • 136
  • 1
  • 4
0

You can save your @change="onChange()" an use watchers. Vue computes and watches, it´s designed for that. In case you only need the value and not other complex Event atributes.

Something like:

  ...
  watch: {
    leaveType () {
      this.whateverMethod(this.leaveType)
    }
  },
  methods: {
     onChange() {
         console.log('The new value is: ', this.leaveType)
     }
  }
biojazzard
  • 563
  • 4
  • 8
  • Please note that watchers are expensive. the above can also be achieved as a computed property, which is far more cheaper. – Ramesh Pareek Oct 06 '19 at 13:54