We will learn How to get data attribute value in Vue JS?
Below is the syntax:
event.target.getAttribute(attribute_name);
Example:
<!DOCTYPE html>
<html>
<head>
<title>How to get data attribute value in Vue JS? - rathorji.in</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="callFunction" data-id="22">Click Me</button>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
message:"Welcome, Please Wait...."
},
methods:{
callFunction: function (event) {
var id = event.target.getAttribute('data-id');
console.log(id);
}
}
});
</script>
</html>
I hope this example helped you.