Skip to content

Instantly share code, notes, and snippets.

@yulvil
Last active August 31, 2023 07:00
Show Gist options
  • Save yulvil/694655e1c9fff66533df5cdecc5ebe31 to your computer and use it in GitHub Desktop.
Save yulvil/694655e1c9fff66533df5cdecc5ebe31 to your computer and use it in GitHub Desktop.
vue.js example with button on-click event
<html>
<head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="example">
<form action="javascript:void(0);">
<button v-on:click="dosomething">Add 1</button>
{{ counter }} {{ message }}
</form>
</div>
<script>
var places = ['World', 'America', 'Europe', 'Asia']
var object = {
message: 'Hello world!',
counter: 0
}
new Vue({
el: '#example',
data: object,
methods: {
dosomething: function (event) {
// `this` inside methods points to the Vue instance
// `event` is the native DOM event
// alert(event.target.tagName);
this.counter += 1;
this.message = 'Hello ' + (places[this.counter%4]) + '!';
}
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment