0

I am doing a to-do list in Vuejs. When I enter a new value in the input field, it seems that I can't make the input value to reset. How can I accomplish this?

I have tried to grab the input value & reset it to an empty string, but I haven't had any luck.

HTML Code:

<div id="app">
  <h1>{{ message }}</h1>
  <form v-on:submit="addNewTodo">
    <input class="input-value" v-model="todo.task" type="text">
    <button type="submit">Add todo</button>
  </form>
  <ul>
   <li  v-for="todo in todos" :class="{ completed: todo.isActive }" @click="$set(todo, 'isActive', !todo.isActive)">
    {{ todo.task }} <span v-on:click="deleteTodo">{{ todo.delete }}</span>
   </li>
  </ul>
</div>

JS Code:

var app = new Vue({
  el: '#app',
  data: {
    message: 'List of things to do today',
    todos: [
      { task: 'Have breakfast', delete:'(x)'},
      { task: 'Go to the gym', delete:'(x)'},
      { task: 'Study Vuejs', delete:'(x)'}
    ],
    todo: {task: '', delete: '(x)'}
  },
  methods: {
    addNewTodo: function(e){

      e.preventDefault();
      this.todos.push( this.todo );

      var inputValue = document.querySelectorAll('.input-value').value;

      inputValue = ''
    },
    deleteTodo: function(){
      this.todos.shift( this.todo )
    }
  }
});

2 Answers 2

2

Your input value is two-way binded with todo.task so you could do the following after adding a new todo task.

this.todo.task = ''
Sign up to request clarification or add additional context in comments.

2 Comments

Do you mind doing a JSfiddle sample? I tried it doesn't seem to work.
I updated my comment, you need to call todo.task with this
1

Try to flush it like :

  addNewTodo: function(e){

      e.preventDefault();
      this.todos.push( this.todo );
      this.todo.task=""
    }

1 Comment

Yup that worked good sir! However, it also deletes the added to-do from the list of to-dos. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.