Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chris1904's avatar

@click on checkbox add/remove data

Hi there,

I currently have the following scenario:

I have multiple checkboxes, once any is clicked, the value of it will get added to an array. If the checkbox is unchecked then the item needs to be removed out of the array again.

    selectAddOn(addOnId) {
        if (! this.selectedAddOns.includes(addOnId)) {
            this.selectedAddOns.push(addOnId);
        }
    }

The following works and it adds them to my selectedAddOns[]. But when the checkbox is checked again, it is not removed. Sure, I could just use else, but...

Unfortunately, the browser behavior is when you click on a <label>, a click event will automatically be triggered on the <input>, so the outer div receives 2 events, one from label, one from input. I am aware that I can work around this by adding @click.prevent on the <label>, but this then will not add my custom checkbox styles.

    <div @click="selectAddOn(index)" class="col-6" v-for="(addOn, index) in categories[categoryId].addOns">
        <label class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input">
            <span class="custom-control-indicator"></span>
            <span class="custom-control-description">{{ addOn.name }} (+ ${{ addOn.price }})</span>
        </label>
    </div>

Any idea on how I can work around this scenario?

Thanks soo much!

0 likes
2 replies
rhymesey's avatar
rhymesey
Best Answer
Level 8

Why don't you give all your checkboxes the same v-model. That way the values will be added / removed automatically when the checkboxes are checked / unchecked.

Check the documentation where it says "Multiple checkboxes bound to the same array"

https://vuejs.org/v2/guide/forms.html#Checkbox

1 like
Chris1904's avatar

thanks! Totally thought of it the wrong way.

Please or to participate in this conversation.