1

I would like to have my text fields empty when I click "add task" button. How could I achieve that?

<template>
  <div class="col-md-6" >
    <div class="card bg-light mt-4">
      <div class="card-header">Task Form</div>

        <div class="card-body">
          <form action="./api/word" method="post" @submit.prevent="addTask()">

            <div class="form-group">
              <input type="text" name="title" v-model="title" placeholder="Local word" class="form-control">
            </div>
            <div class="form-group">
              <input type="text" name="second_title" v-model="second_title" placeholder="Foreign word" class="form-control">
            </div>
            <div class="form-group">
              <input type="submit" value="Add Task" class="btn btn-info">
            </div>
          </form>
        </div>

      </div>
  </div>
</template>
export default {
  data() {
    return {
      title: '',
      second_title: ''
    }

  },
  mounted() {
    console.log('Component mounted.')
  },
  methods: {

    addTask() {
      Event.$emit('taskCreated', {
        title: this.title,
        second_title: this.second_title
      });
      axios.post('./api/word', {
        title: this.title,
        second_title: this.second_title,

      })
      this.title = '';
      this.second_title = '';


    }
0

1 Answer 1

2

It's very simple, in the addTask() just add event as parameter and in the end of the function you write: event.target.reset();

Code:

<template>
  <div class="col-md-6" >
    <div class="card bg-light mt-4">
      <div class="card-header">Task Form</div>

        <div class="card-body">
          <form action="./api/word" method="post" @submit.prevent="addTask">

            <div class="form-group">
              <input type="text" name="title" v-model="title" placeholder="Local word" class="form-control">
            </div>
            <div class="form-group">
              <input type="text" name="second_title" v-model="second_title" placeholder="Foreign word" class="form-control">
            </div>
            <div class="form-group">
              <input type="submit" value="Add Task" class="btn btn-info">
            </div>
          </form>
        </div>

      </div>
  </div>
</template>

And there:

export default {
  data() {
    return {
      title: '',
      second_title: ''
    }

  },
  mounted() {
    console.log('Component mounted.')
  },
  methods: {

    addTask(event) {
      Event.$emit('taskCreated', {
        title: this.title,
        second_title: this.second_title
      });
      axios.post('./api/word', {
        title: this.title,
        second_title: this.second_title,

      })
      this.title = '';
      this.second_title = '';

      event.target.reset();
    }

Hope it works for you ; )

Sign up to request clarification or add additional context in comments.

1 Comment

TypeError: e.target.reset is not a function

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.