1

enter image description here

I want some thing like this, If I hit "Add Another" button another set of input boxes should render below this set and this set's "Add Another" button should change to the "Remove This School" and the maximum number of input rows should limited to the 5.

I hope you understand my requirement.What I want is that the user can enter 5 schools or less than 5 schools.If he accidentally add some wrong information he should have a remove button to remove that record.How do I achieve this using Vue JS. This is my code.

<template>
  <div id="app">
    <p>Please enter the schools you attained (Maximum five)</p>
    School Name : <input type="text" name="" value="">
    Location  : <input type="text" name="" value="">
    <button type="button" name="button" @click="addAnotherInputBox">Add Another</button>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      counter:0

    }
  },
  methods:{
    addAnotherInputBox(){

    }
  }
}
</script>

1 Answer 1

3

Here you go, although you should have filled the logic of your add button, but you can do that by your own

Here is a working fiddle: https://jsfiddle.net/Sletheren/Lo7t0myL/

<template>
  <div id="app">
    <p>Please enter the schools you attained (Maximum five)</p>
    School Name : <input v-model="name" type="text" name="" value="">
    Location  : <input v-model="location" type="text" name="" value="">
    <button type="button" name="button" @click="addAnotherInputBox">Add Another</button>
    <ul class="schools">
        <li v-for="school in schools">
            {{school.name}} <button @click="removeSchool(school)">remove</button>
        </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      counter:0,
      name: '',
      location:'',
      schools: []
    }
  },
  methods:{
    addAnotherInputBox(){
        // your logic here...
       if(this.counter>4){
           alert('you should delete one first! only 5 allowed')
       }else{
            const school = {
                name: this.name, //name here
                location: this.location //location here
            }
            this.counter++;
            this.schools.push(school)
            this.name='';
            this.location=''
       }
    },
    removeSchool(school) {
        this.schools.splice(this.schools.indexOf(school),1);
        this.counter--;
    }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you a lot. I highly appreciate your answer. I learnt a lot from this. Thanks again. =)

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.