Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear my input text in VueJS after a button is clicked?

I'd like the text in my input box to clear once the enter button it pushed and the addTask function is added to the list. I tried document.getElementById("inp").innerHTML = "" and it didn't work. How can I do this?

HTML:

<div id="todo">
    <h1>To-Do List</h1>
    <section>
        <input type="input" placeholder="what do you need to do?" v-model="newTask" v-on:keyup.enter="addTask" id="inp">
    </section>

    <ul>
        <li v-for="task in todoList">
            <label>{{ task }}</label>
            <button type="button" v-on:click="removeTask(task)">X</button>
        </li>
    </ul>

</div>

VueJS:

var todo = new Vue({
    el: 'div#todo',
    data: {
        newTask:'',
        todoList: []
    },
    methods: {
        addTask: function() {
            var task = this.newTask
            this.todoList.push(task)
        },
        removeTask: function(task) {
            var index = this.todoList.indexOf(task)
            this.todoList.splice(index, 1)
        }

    }
})
like image 937
smontoro14 Avatar asked Dec 15 '25 02:12

smontoro14


1 Answers

clear your model:

addTask: function() {
                var task = this.newTask
                this.todoList.push(task)

                this.newTask = ''
            }
like image 193
enno.void Avatar answered Dec 16 '25 18:12

enno.void