1

I am having a custom checkbox in vue which dynamically gets generated using a v-for loop.I need to get the ids of every checked checkbox and store it in the form of string with comma separated. For EG: "Male , Female, Other" - these are the checked values.

But as i am generating checkboxes using a v-for the checked values are getting overridden. Below is the fucntion that gets called on checkbox click.

updateCheckAll(e) {
 let checkboxes = [];
 if(e.target.checked) {
   checkboxes.push(e.target.id);
 }
  checkboxes.join(',');

}

HTML - These are custom checkboxes.

<formfield v-for="(option,index) in getCheckBoxData" :key="index" :label="option.value"> <checkbox :id="option.key" :value="option.value" @change="updateCheckAll($event)"></checkbox> </formfield>

2 Answers 2

2

You need to keep state of checked checkboxes:

data() {
  return {
    checked: [],
  }
},

//template
<checkbox :id="option.key" :value="option.value" v-model="checked">

That is all you need to do 😉

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

1 Comment

but the thing is custom components does not support v-model thus v-model value is never stored in checked.
2

You can check the vue documentation for more details https://v2.vuejs.org/v2/guide/forms.html

    <input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
    <label for="jack">Jack</label>
    <input type="checkbox" id="john" value="John" v-model="checkedNames">
    <label for="john">John</label>
    <input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
    <label for="mike">Mike</label>
    <br>
    <span>Checked names: {{ checkedNames }}</span>

   new Vue({
      el: '...',
      data: {
        checkedNames: []
      }
    })

Code pen https://codepen.io/tonytomk/pen/NWxbeOB

2 Comments

Custom Components does not support v-model in vue. So , i am trying to capture value using a event, which get overridden everytime checkbox is clicked as the checkboxes are generated dynamically.

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.