VueJS How to Get Checked Radio Button Value
Hello devs, today I'll show you how to get checked radio button value in a VueJS application.
Note: Tested on VueJS 2.6.14
We'll use an onChange() method which will fire when we change/select a radio button. Let's look at the below source code.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>VueJs - Get Checked Radio Button Value - shouts.dev </title>
</head>
<body>
<div id="app">
<label><input type="radio" name="category_id" v-model="radio_btn" @change="onChange" value="1"> Laravel</label>
<label><input type="radio" name="category_id" v-model="radio_btn" @change="onChange" value="2"> Codeigniter</label>
<label><input type="radio" name="category_id" v-model="radio_btn" @change="onChange" value="3"> Vue JS</label>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data : {
radio_btn : ''
},
methods: {
onChange() {
console.log(this.radio_btn);
}
}
})
</script>
</body>
</html>
It'll produce the below result if we change/select a radio button.
That's it for today. I hope you've enjoyed this tutorial. If you found any difficulties o questions, catch me in the comment sections. Thanks for reading. ๐