Having trouble resetting a checked checkbox label in Vuejs?

uncheck: function(checkedName) {
    this.checkedNames = this.checkedNames.filter(name => name !== checkedName);
  },
  uncheckall: function(event) {
    this.checkedNames = [];
  },

  checkedInput(event) {
    if (this.checkedNames.includes(event.target.value)) {
      this.uncheck(event.target.value)
    } else {
      this.checkedNames.push(event.target.value)
    }
  },
<div class="checkbox-alignment-form-filter">
  <input type="checkbox" id="H11" class="vh-product" value="1" v-model="checkboxes[0]"  @click="checkedInput" />
  <label class="productlist-specific" for="HR1">H1</label
                >
              </div>
              <div class="checkbox-alignment-form-filter2">
                <input
                  type="checkbox"
                  id="E0A"
                  class="vh-product"
                  v-model="checkboxes[1]"
                  value="E0A"
                  @click="checkedInput"
                />
                <label class="productlist-specific" for="E0A">E0A</label>
</div>

</div>


<li @mouseover="mouseOver" @mouseleave="mouseOut" @click="uncheck(checkedName)" class="filtersapplied-productlist2" v-for="checkedName in checkedNames" :key="checkedName">
  {{ checkedName }}
  <span class="filtertag-plp" v-show="close" aria-hidden="true">&times;</span
            >
          </li>
          <li
            class="filtersapplied-productlist4"
            @mouseover="mouseOver"
            @mouseleave="mouseOut"
            @click="uncheckall"
            v-show="checkedNames != ''"
          >
            CLEAR ALL
          </li>

Having difficulty in deselecting the selected checkbox labels. The selected label displays properly but clearing all checkboxes using the "Clear All" option does not work as expected.

Not sure where the issue lies, whether it's in the checkedInput method or in the list item where a specific ID needs to be passed for clearing.

This code is based on a reference I found, however, after some modifications, the desired functionality is not achieved. Reference: https://jsfiddle.net/8xom729c/

Answer №1

It is unnecessary to bind the v-model directly to an index of the checkboxes array.

Instead of:

v-model="checkboxes[0]" // also checkboxes[1], checkboxes[2], etc

Use:

v-model="checkboxes"

Vue will handle the binding of checkboxes automatically, as explained in thedocumentation.

If you bind directly to indexes, the array will be filled with undefined/true instead of the checkbox values, causing potential conflicts and issues when resetting their state.

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

Using Javascript to automatically submit a form when the enter key is pressed

Can anyone help me with a password form issue? I want the enter key to trigger the submit button but it's not working for me. I understand that the password can be viewed in the source code, this is just for practice purposes. <!DOCTYPE html> & ...

Preserve the existing value and then check it against the updated value of a variable within JavaScript

I utilized an API that supplies me with information in JSON format, retrieved the price of a specific currency, and presented it on a screen using JavaScript. I encapsulated this process within a function that dynamically updates the information at set int ...

Discovering the operating system and architecture type for my browser using Node.js - what steps should I take?

I am currently developing a Node.js app that serves as an API microservice. I need to retrieve the operating system and architecture type from a GET request made by my browser. One example could be: "Windows NT 10.0; Win64; x64" ...

Issue with Rails 5 Bootstrap 3 Modal and Ajax functionality not functioning as expected

In the process of developing an app in Rails 5 and utilizing Bootstrap 3 as the framework. A custom user controller has been set up to enable admins create, edit, and delete users. Modals are being implemented for the user creation form, where the modal i ...

What would be the best choice between using emit or scope-slot in this particular scenario?

The main content shown in the screenshot consists of a search feature and a sidebar component. By clicking on the search drop-down, a list of items appears. Selecting an item from this list will update the content displayed in the sidebar. If the user cli ...

What is the response of Express when it encounters numerous identical asynchronous requests from the same origin?

Currently, I am utilizing Express.js for my project. There is an async function that performs a task that can take anywhere from 20 to 30 seconds to complete. Once the task is done, it increases a user's counter in the database. However, users are req ...

The function is coming back with a value of undefined

I need some assistance with a function I have below. This function is meant to download files one by one and move them to a directory called templates. At the end, it should return the length of the directory. However, it seems to be returning an undefined ...

How can I access the id_lang variable in React JS from outside its scope?

How do I access the 'id_lang' variable outside of the render function in order to pass it down for checking? const Navbar = () => { const getID = async (id) => { let id_lang = id; console.log(id_lang); } ret ...

Steps for displaying a deeply nested array and string within an object that is nested further in an array

I've been troubleshooting an issue in my Next.js code where I'm trying to render data called items. My goal is to display the descriptions in an array as a list and display the description that is a string as it is. However, I cannot use the map ...

Can you explain how the functionality of setState operates within React in this specific situation?

I've been working on updating the state of an object in my array. I managed to get it done, but I'm a bit confused about how it works. toggleVisited = countryCode => { var countries = [ ...this.state.countries ]; var countryToChange = c ...

Iterate through the .json file and add markers to a leaflet map

Apologies for the basic question, but I'm struggling with understanding JavaScript and json files. I have a .json file that updates server object locations every 5 seconds, and I want to plot these coordinates on a map using leaflet for staff access. ...

Transition between different segments

Currently working on a sapui5 fiori app, I find myself utilizing multiple fragments based on the mode (read, create, edit). However, I am stuck on how to smoothly navigate from one fragment to another, and more importantly, how to seamlessly return to th ...

What occurs when the update function returned by React.useState() is invoked?

Just recently, I delved into the world of React hooks and decided to implement a small feature using them. The feature enables hidden texts to be displayed when users click on hyperlinks. Although I managed to make the code work, it appears that there are ...

Ensure the detection of 404 error status using jQuery

My current approach involves using this code snippet to retrieve data from Twitter through its API: $.ajax({ url : apiUrl, cache : false, crossDomain: true, dataType: "jsonp", success : f ...

"The printing function of the "Print page" button appears to be malfunctioning

I am having some trouble with my JavaScript code for a "print page" button in an HTML document. The button appears and is clickable, but it doesn't actually print the page as intended. I keep receiving 3 errors related to the `document` object being u ...

Is there a way that I can move data input from one page to another using a form table?

Currently, I am developing a practice task manager using Bootstrap-4 and Vue.js. Although this is all relatively new to me, I have made significant progress. The only thing left to do is figure out how to transfer the input data from one html page to anoth ...

Check if a Discord.js bot responds based on whether a user holds a designated role

I'm looking to make my bot respond to a specific role. If the user does not have that role, I want the bot to reply with a message saying "You are not allowed to perform this command." Here is the code snippet I am using: client.on("message", (message ...

javascript selecting window location strings

Hey there, http://localhost/estamo/asset.php?aname=VklQIFBsYXph&di=Ng== I have this URL and I want to retrieve it using Javascript: var locat = window.location.href; $.get("enviaramigo.php?email="+$("#email").val()+"&url="+locat, function(h ...

Ajax request and the Ghostery extension in Firefox

I've implemented the following code to detect ad blockers like Ghostery: <script> var request = new XMLHttpRequest(); request.onreadystatechange = function() { if(request.readyState === 4 && request.status === 200 ) { ...

Exploring the Depths of Multidimensional JSON Arrays in PHP

I am currently working on developing a web-based file manager that allows me to organize, view, create, edit, and delete folders and files. In order to store information about these folders, files, and subfolders, I am in need of an appropriate data struct ...