[Vuejs]-How to clear form after submit in vuejs

0๐Ÿ‘

โœ…

Well it was simple and I did following code for emptying the form.

addtodirectory(event) {
      this.$Progress.start();
      this.form.post('api/addtodirectory');
    //  document.getElementById("form-directory").reset();
     console.log('durgesh');
      // document.getElementsByName('name').value = '';
      Toast.fire({
         type: 'success',
        title: 'Directory Updated successfully'
          })

        this.form.name = "";
        this.form.profession ="";
        this.form.address="";
        this.form.city = "";
        this.form.state = "";



      this.$Progress.finish();


    },

after submitting the form I did not used the form. so after doing this I emptied the form.

0๐Ÿ‘

Simply empty your form object after form submit.

form: new Form({
    name : '',
    address:'',
    profession:'',
    city:'',
    state:''
  })

0๐Ÿ‘

Or in a one-liner:

Object.keys(form).forEach(v => form[v] = "")

instead of:

this.form.name = "";
this.form.profession ="";
this.form.address="";
this.form.city = "";
this.form.state = "";

Leave a comment