title: Vue.js - click events and "this"
body: <p>I have a todo list in vue and I'm using <code>pop()</code> to clear out/delete list items. See the code in question below:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// components
  Vue.component('todoitem', {
    template: "&lt;li&gt;Test Item&lt;/li&gt;"
  })
  
  // app code
  var app = new Vue({
    el: '#app',
    data: {
      todos: [
        { text: 'Sample Item 1' },
        { text: 'Sample Item 2' },
        { text: 'Sample Item 3' }
      ],
      button: {
        text: 'Hide'
      },
      seen: true
    },
    methods: {
      addItem: function() {
        let item = document.getElementById("list-input").value;
        let error = document.getElementById("error");
        if (item == "") {
          error.style.display = "block";
        } else {
          app.todos.push({ text: item });
          error.style.display = "none";
        }
      },
      removeItem: function() {
       this.todos.pop();
      },
      toggleSeen: function() {
        app.seen = !app.seen;
        app.button.text = app.seen ? 'Hide' : 'Show';
      }
    }
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.todo-list {
    list-style-type: square;
  }
  
  .todo-list__delete {
    display: none;
  }
  
  li:hover .todo-list__delete {
    display: inline-block;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"&gt;&lt;/script&gt;
  
  &lt;div id="app"&gt;
    &lt;ul class="todo-list"&gt;
      &lt;li v-for="todo in todos"&gt;
        {{ todo.text }}
        &lt;a v-on:click="removeItem" class="todo-list__delete" href="#"&gt;Delete&lt;/a&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  
    &lt;input type="text" id="list-input"&gt;
    &lt;input type="submit" id="list-submit" v-on:click="addItem"&gt;
    &lt;span id="error" style="color: red; display: none;"&gt;Please Enter Text&lt;/span&gt;
  
    &lt;ul&gt;
      &lt;todoitem&gt;&lt;/todoitem&gt;
    &lt;/ul&gt;
  
    &lt;h2 v-if="seen"&gt;SEEN&lt;/h2&gt;
    &lt;button id="hide-seen" v-on:click="toggleSeen"&gt;{{ button.text }}&lt;/button&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The expected behavior is that when delete is clicked, it invokes the removeItem function and with the usage of <code>this</code> in that function it deletes the selected item. However what actually happens is it just deletes nodes starting from the bottom. </p>
  
  <p>I <em>thought</em> the issue is that with <code>this</code> I'm actually referencing the delete link and not actually the <code>&lt;li&gt;</code> element I'm trying to remove. So I tried both:</p>
  
  <pre><code>removeItem: function() {
      this.todos.parentElement.pop();
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>removeItem: function() {
      this.parentElement.todos.pop();
  }
  </code></pre>
  
  <p>With no luck.</p>
  
  <p>How does <code>this</code> work in Vue?</p>