What is the best way to clear an input value in Vuejs after it has been submitted?

Could you help me with my to-do list in Vuejs? I'm having trouble resetting the input value when a new item is added. Any suggestions on how to achieve this?

I've attempted to retrieve the input value and set it to an empty string, but unfortunately, it hasn't worked for me.

This is the HTML code I am working with:

<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>

Here's the JavaScript code as well:

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 )
    }
  }
});

Answer №1

By utilizing two-way binding, the input value is synced with the todo task. This allows you to easily clear the input field after adding a new todo task.

this.todo.task = ''

Answer №2

Attempt to clear it by performing the following:

  addNewTodo: function(e){

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to incorporate HTML code into a fullCalendar 4 event title?

Is it possible to add HTML content to an event title using eventRender in FullCalendar version 4? document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new ...

What is the best method for choosing these elements and surrounding them with a wrapper?

I need to style a title with two radio inputs by wrapping them in a form: <p><strong>Country</strong></p> <div class="radioWrapper"> <span class="label">Canada</span> <span class="radio"> ...

Difficulty arises when Jest tests struggle to interpret basic HTML tags within a React Component

When running test runs, issues arise when using standard HTML tags with Jest. My setup includes Babel, Webpack, Jest, and React Testing Library. To enable jest, I have installed a number of packages: "@babel/plugin-proposal-class-properties": "7.8.3", "@ ...

Is it possible to transfer a URLFetchApp.fetch request from the Google Apps Script side to the JavaScript side?

My current task involves parsing an XML document tree upon clicking a button. The XML file is obtained using a lookup function that requires two values ("id" and "shipping") to be inserted into the appropriate URL. Then, the data retrieved is parsed using ...

Interacting with a JavaScript alert by clicking the OK button using C# in a web browser control

In my C# Windows Forms program, I am utilizing a webbrowser control to navigate multiple pages of a website and interact with forms to carry out transactions. I initially tried using httpwebrequest and webclient, but encountered challenges with cookies and ...

The function Router.use() is in need of a middleware function, but instead received an undefined

A snippet of code from my index.js file looks like this: ... import userRoutes from './src/routes/userRoutes'; import invoicesRoutes from './src/routes/invoicesRoutes'; import authMiddleware from "./src/middlewares/authMiddleware"; ... ...

Undefined method error encountered within Vue.js nested ref structure

My component structure is three levels deep and it's set up like this: - container - section-1 // section1Ref - form-1 // form1Ref The submit method in the container component will call the submit method in section-1 using this.$refs.section1R ...

Having trouble removing items from the data grid list in react material-ui, any thoughts on what might be causing the issue?

I'm facing an issue with deleting items from a basic list of customers rendered using material UI DataGrid. Even though I can retrieve the specific customer id when logging to the console, I am unable to delete the item from the list. You can view my ...

Error: The function app.all is not defined

This is a basic Angular page that I have set up. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> ...

Content not appearing in ng repeat loop

I'm facing a basic issue that I can't seem to solve - my code isn't working as expected: <article id="desktop"> <h3>Content: </h3> <ul> <li ng-repeat="x in storage"> name: {{x.name}} ...

Using JQuery to delete data from a cookie

I have a delicious cookie that I want to savor before removing one element from it based on the widget ID. if (thisWidgetSettings.removable) { $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) { ...

When parameters are added to one route, all other routes cease to function properly

After I added a parameter to one of the routes, all the other routes stopped rendering correctly (i.e., router-view is not working at all). The route /download/:id works as expected. Am I missing something in the setup? I tried leaving and removing the /do ...

Utilizing useEffect Hooks to Filter Local JSON Data in React Applications

For my engineering page, I have been developing a user display that allows data to be filtered by field and expertise. Fields can be filtered through a dropdown menu selection, while expertise can be filtered using an input field. My initial plan was to ...

Using ng-checked directive with a radio button in AngularJS

Within my angular application, I have implemented a loop to iterate through a collection and display the records using input type="radio". <tr ng-repeat="account in vm.pagedAccounts.items" ng-class="{ 'highlight': (account.row ...

Struggling to perform a basic http GET request in Angular 2

Attempting to create a basic http GET request in my Angular2 app: this.http.get("https://mybaseurl.com/hello") .map(res => res.json()) .subscribe( function(response) { console.log("Success Response" + response)}, function(error) { conso ...

Utilizing a router to control a GET request for accessing an image file

Currently utilizing node.js in conjunction with the express framework, I am attempting to initiate a get request for an image by appending it to the body through $('body').append('<img src="images/image.gif?34567">'). Despite rece ...

Exploring the differences between utilizing request.body in building RESTful APIs with Django versus Node.js

As I dive into learning the Django framework, my main aim is to leverage this knowledge in creating a rest api. Although I've looked into using django-rest framework, my current job necessitates a focus on Django specifically. In my journey so far, I ...

Utilize JSON categories to assign groups to TextFields or Selects according to a JSON data attribute

I have retrieved multiple JSON groups from an API, each containing one or more questions objects. My goal is to display each question along with its corresponding response in a MUI TextField or Select component, based on the value of QuestionType. Current ...

Is it possible to determine when a Vue.js component instance has been successfully mounted?

Can we access a boolean in each component instance to determine when a component has been mounted? Something along the lines of: <template> <div> <span v-if="$mounted">I am mounted</span> <span v-if="$created">I am ...

What is the best way to clean HTML in a React application?

I am trying to showcase HTML content on the React front end. Here is the input I have: <p>Hello/<p> Aadafsf <h1>H1 in hello</h1> This content was written using CKEditor from the Admin side. This is how it appears on the React fro ...