Vue.js - exercise2

About events

by hyeyoon

HTML

<script src="https://npmcdn.com/vue/dist/vue.js"></script>

<div id="exercise">
    <!-- 1) Show an alert when the Button gets clicked -->
    <div>
        <button v-on:click="showAlert">Show Alert</button>
    </div>
    <!-- 2) Listen to the "keydown" event and store the value in a data property (hint: event.target.value gives you the value) -->
    <div>
        <!-- <input type="text" v-on:keydown="storeValue"> -->
        <input type="text" v-on:keydown="value = $event.target.value">
        <p>{{ value }}</p>
    </div>
    <!-- 3) Adjust the example from 2) to only fire if the "key down" is the ENTER key -->
    <div>
        <!-- <input type="text" v-on:keydown.enter="fireEnter"> -->
        <input type="text" v-on:keydown.enter="value = $event.target.value">
        <p>{{ value }}</p>
    </div>
</div>

JavaScript

new Vue({
        el: '#exercise',
        data: {
            value: ''
        },
        methods: {
        	showAlert: function() {
          	alert('Alert!!');
          },
          storeValue: function(event) {
          	this.value = event.target.value;
          },
          fireEnter: function(event) {
          	this.value = event.target.value;
          }
        }
    });