how to uncheck a checkbox using Vue.js

When the next button is clicked, I want to remove the checked status of the checkbox without using Vue. Here is a demo code snippet:

new Vue({
  el: "#app",
  data: {
    id:0, 
    items: [
    {'id':1,'name':'chk-a','options':['a1','b1','c1','d1']},
    {'id':2,'name':'chk-b','options':['a2','b2','c2','d2']},
    {'id':3,'name':'chk-c','options':['a3','b3','c3','d3']},
    {'id':4,'name':'chk-d','options':['a4','b4','c4','d4']},
    ]
  },
  methods: {
    next: function (id) {
      if(id<this.items.length){
        this.id++ 
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>
<div id="app">
  <h1>Checkbox</h1>
  <div v-for="item in items[id].options">
    <input type="radio" name="chk" :id="id">
    <label for="two">{{item}}</label>
  </div>
  <button @click="next(id+1)">Next</button>
</div>

Answer №1

It appears that you have a form with four sections where the user needs to select one option from each section. To achieve this, consider creating a field like selected_option and using v-model:

new Vue({
  el: "#app",
  data: {
    id:0, 
    items: [
    {'id':1,'name':'chk-a','options':['a1','b1','c1','d1'], 'selected_option': ''},
    {'id':2,'name':'chk-b','options':['a2','b2','c2','d2'], 'selected_option': 'c2'},
    {'id':3,'name':'chk-c','options':['a3','b3','c3','d3'], 'selected_option': ''},
    {'id':4,'name':'chk-d','options':['a4','b4','c4','d4'], 'selected_option': ''},
    ]
  },
  methods: {
    next: function (id) {
      if(id<this.items.length){
        this.id++ 
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>
<div id="app">
  <h1>Checkbox</h1>
  <div v-for="item in items[id].options">
    <input type="radio" :name="items[id].name" :id="id" v-model="items[id].selected_option" :value="item">
    <label for="two">{{item}}</label>
  </div>
  <button @click="next(id+1)">Next</button>
</div>

Please note that 'c2' is pre-selected in the second section as an example of default values for v-model with checkboxes. You can easily retrieve these values later by iterating through the items and accessing the corresponding selected_option values.

Similarly, you can uncheck an option by setting its selected_option value to ''.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Merge the capabilities of server-side exporting and client-side exporting within Highcharts for Vue

While working with Highcharts' export server to download charts as images, I encountered a challenge. I needed to implement the client-side (offline) exporting feature in a single chart due to the large number of data points. However, after enabling c ...

Dynamically add index to attribute as it updates

Having an issue with my dynamic button element: <button v-on:click="changeRecord(element)" v-b-modal.modal-5>Aendern</button> This button is generated dynamically within a v-for loop. Instead of manually including the attribute name like v-b- ...

"Designing with Vue.js for a seamless and adaptive user experience

I'm looking to customize the display of my data in Vuejs by arranging each property vertically. Instead of appearing on the same line, I want every item in conversationHistory to be displayed vertically. How can I achieve this in Vuejs? Can anyone off ...

Avoid refreshing the page when clicking on an anchor tag in Vue.js

I have the code below in my Vue file. The problem I am encountering is that the page reloads whenever I click on the link. I have tried using event.preventDefault() but it did not solve the issue. Can someone please help me identify what I am doing wrong ...

Do not attempt to log after tests have finished. Could it be that you overlooked waiting for an asynchronous task in your test?

Currently, I am utilizing jest in conjunction with the Vue framework to create unit tests. My test example is successfully passing, however, I am encountering an issue with logging the request. How can I resolve this error? Is there a mistake in my usage o ...

Leverage Vue3's v-html feature without the need for additional wrapping tags by using script

Is it possible to use Vue's v-html directive within a Vue 3 <script setup> setup without needing an additional wrapping tag? I am looking to achieve something similar to the following: <script setup> const html = ref(`<pre></pre& ...

Running a function in a different vue file from an imported file - A step-by-step guide

Learning the ropes of vue.js, I am working with two separate vue files: First file: import second from second.vue; export default{ component: {second}, data() {return{ data: 'data', }} methods: { function f1{ console.log(t ...

What is the best way to create dynamic .env files that can easily adapt to different environments instead of

Having multiple .env files (one for Vue and one for Laravel) with 'localhost' hard coded in them is causing accessibility issues from other computers on my network. It would be beneficial to have this set up dynamically, except for production. F ...

How can you access the most recent user information in Vuex following a user profile update?

Struggling with making a user's data "reactive" in Vuex after updating their profile? Here's the situation: In my App.vue, I check a user's details using the created() hook as follows: async created() { await this.$store.dispatch( ...

Utilizing Google Analytics with Laravel Jetstream

Currently, I'm facing challenges with setting up Google Analytics to properly report data with the InertiaJS stack on Laravel Jetstream. My goal is to track individual page visits within this single-page application, but I'm uncertain about the a ...

Passing a variable as a property to a nested child component in Vue.js

I am curious about how to efficiently pass variables to nested components. Within my setup, I have a total of 3 components: Main Secondary Tertiary All of these components share a common variable (referred to as sharedVar). If I want to avoid using Vue ...

Understanding how to handle errors in Javascript using promises and try/catch statements

Seeking assistance because I'm having trouble grasping a concept... Here's the code snippet: // AuthService.js login(user) { return Api.post('/login', user); }, // store/user.js async login(context, user) { try { let ...

When updating a form, it is essential to display select box data from the database effortlessly. It is also important to ensure that the select box data's id is

In my current project, a quasar (vuejs) app, I am faced with an issue related to an update form functionality. The challenge I am encountering is that when I submit the form without making any changes to the select box, it submits the label as a string ins ...

Personalized design created using v-for

I have an array being passed through v-for and I want to use the values within the "style" attribute. Essentially, I need to append the value from v-for to style:"left"+EachValue+"px", but I'm having trouble with the syntax. I'm unsure if this ap ...

Copy both the image and JSON object to the clipboard

I am attempting to utilize the clipboard API to write an image and JSON object to the window clipboard. I am working with Vue and Electron and have successfully written an image and plain text, but I encounter an error when trying to write a JSON object: ...

What steps can be taken to resolve the deprecated error for isVueInstance in vue.js2?

Utilizing vue-jest for test cases in vue.js2 involves working with a component named Register.vue. The test cases are written in Register.spec.js, and when running npm t, everything is functioning correctly. However, there are some errors being encountered ...

What is the best approach to integrate a JQuery-powered widget into a Vue.js module for seamless use?

I have a group of colleagues who have started developing a complex web application using Vue.js. They are interested in incorporating some custom widgets I created with JQuery in the past, as re-creating them would be time-consuming and challenging. While ...

What is the best way to conceal a portion of the input value within a v-text-field?

The Challenge Ahead- This particular v-text-field has been created to enable users to manually input and modify their name on the website. However, for security reasons, I am looking to conceal all parts of the input value except the last character of the ...

Issue with Dynamic Image Path in Require Function: Unable to locate the relative module

I've been struggling with an error in VueJs require function for the past two days. I'm attempting to pass a prop to the Home component and then display the image. Home.vue <template> <BlogPost :post="welcomeScreen"/> <B ...

Tips for integrating TypeScript with Vue.js and Single File Components

After extensive searching online, I have struggled to find a straightforward and up-to-date example of setting up Vue.js with TypeScript. The typical tutorials out there either are outdated or rely on specific configurations that don't apply universal ...