Deselect the Angular checkbox

In my project, I need to create a list of checkboxes from an array using classes. There will also be a reset button that, when clicked, will reset all selections. How can I achieve this? Thank you.

    $scope.classList=['Class1','Class2','Class3','Class4'];
$scope.selectedClasses={};
    $scope.selectedClasses.usall=false;
    $scope.selectedClasses.sall=false;          
    $scope.selectedClasses.classListchecked=false;
    $scope.getSubscribedClassList = function(classNam) {

            if(classNam==="All"){
                 $scope.selectedClasses.usall=false;
                 $scope.selectedClasses.classListchecked=false;
            }


                else{
                    $scope.selectedClasses.usall=false;
                    $scope.selectedClasses.sall=false;
                    $scope.selectedClasses.classListchecked.push(classNam)

            }
        }

Here is the link to my fiddle: http://jsfiddle.net/alokranjan39/49cc14yk/38/

Answer №1

After making some adjustments, here is the updated fiddle (you can view it here)

Here is the modified HTML:

<div ng-controller="MyCtrl">

  <input type="checkbox" ng-model="masterSelect" ng-click="getSubscribedClassList('All')"> UnSelectAll

  <li ng-repeat="class in classList">
    <span>
      <input type="checkbox" ng-model="class.isSelected" ng-disabled="class.students.length==0">
      {{class.name}} </span>
  </li>
</div>

And the updated JavaScript code:

function MyCtrl($scope) {
  $scope.classList = [{
    isSelected: false,
    name: 'Class1'
  }, {
    isSelected: false,
    name: 'Class2'
  }, {
    isSelected: false,
    name: 'Class3'
  }, {
    isSelected: false,
    name: 'Class4'
  }];

  $scope.getSubscribedClassList = function(classNam) {

    if (classNam === "All") {
      angular.forEach($scope.classList, function(value, index) {       
        value.isSelected = $scope.masterSelect;       
      });
    } else {
      //this part is not necessary. You can get checked items from $scope.classList where isSelected === true

    }
  };
}

To retrieve a list of selected items, loop through $scope.classList and check for items where isSelected === true

Answer №2

Check out the updated fiddle here - http://jsfiddle.net/49cc14yk/43/

This is the HTML code:

<div ng-controller="MyCtrl">
    <input type="checkbox" ng-model="selectedClasses" ng-click="getSubscribedClassList()">
    UnSelectAll                 

    <li ng-repeat="class in classList">
        <span>
            <input type="checkbox" ng-model="class.value">
                {{class.label}}
        </span>
    </li>
</div>

And this is the JS code:

function MyCtrl($scope) {
    $scope.classList=[{
        label: 'Class1', value: false
    }, {
        label: 'Class2', value: false
    }, {
        label: 'Class3', value: false
    }, {
        label: 'Class4', value: false
    }];

    $scope.getSubscribedClassList = function() {
      $scope.classList.forEach(function(obj) {
          obj.value = $scope.selectedClasses;
      });
    }
}

Answer №3

Check out the updated fiddle for the array as well - http://jsfiddle.net/49cc14yk/45/

Here is the HTML code snippet:

<div ng-controller="MyCtrl">
    <input type="checkbox" ng-model="selectAll" ng-click="getSubscribedClassList('ALL')">
    UnSelectAll                 
    <li ng-repeat="class in classList">
        <span>
            <input type="checkbox" name="selectedClasses[]" value="{{class}}"
            ng-checked="selectedClassList.indexOf(class) > -1" ng-click="getSubscribedClassList(class)">
            {{class}}
        </span>
    </li>
</div>

And here is the JS code snippet:

function MyCtrl($scope) {
    $scope.classList = ['Class1','Class2','Class3','Class4'];
    // Initialise below array as per your requirement, maybe blank
    $scope.selectedClassList = ['Class1','Class2'];

    $scope.getSubscribedClassList = function(value) {
        if(value == 'ALL') {
            $scope.selectedClassList = [];
            if($scope.selectAll) {
                $scope.selectedClassList = Object.create($scope.classList);
            }
        } else {
            var idx = $scope.selectedClassList.indexOf(value);
            if (idx > -1) {
                $scope.selectedClassList.splice(idx, 1);
            } else {
                $scope.selectedClassList.push(value);
            }
        }
    };
}

It's recommended to use objects in this scenario, so I have included that solution as well.

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

Update the background URL of a div element using an HTML tag

My question is about a CSS div with set dimensions and background that I want to change on hover. I am aware that this seems like a simple task, but what I really want to do is retrieve the background sources from within the HTML tag itself. For example: ...

Remove feature in HTML file upload form

I have a basic upload form set up like this: <form method="post" action="" enctype="multipart/form-data"> <table border="0"> <tr> <td>Select XML file to upload ...

How can I display the value of a clicked text in the search input box using AngularJS and Bootstrap?

I have a search box that functions like Google, with a list of text values displayed below. I would like to be able to click on any of these text values and have it automatically appear in the search box. Is there a way to achieve this? ...

Node.js used to create animated gif with unique background image

As I navigate my way through the world of node.js and tools like express and canvas, I acknowledge that there might be errors in my code and it may take me some time to grasp certain concepts or solutions. My current project involves customizing a variati ...

Activate Bootstrap tooltip when input element is focused, and then when the next input element is selected

I'm trying to activate a tooltip on an input element within a table. My goal is to trigger the tooltip when either that specific input element or the adjacent input element in the table are focused. Below is the HTML structure: <td class="fea ...

Tips for integrating jwt token into axios request

I am facing an issue with my backend endpoint. I can successfully retrieve a list of customers using jwt token on Postman, but when I try to fetch the list from a React app using axios get request, it fails. After reading through this question, I implemen ...

Using Vue.js to make an AJAX request to an API that returns a list in JSON format

I am attempting to utilize an AJAX call with the free API located at that returns a list in JSON format. My goal is to display the result on my HTML using Vue.js, but unfortunately, it doesn't seem to work as expected. This is my JavaScript code: va ...

Press the button to reveal the table - jQuery/JavaScript

Can you please provide guidance on how to hide the inner HTML table when the page loads and then display the table with results only after clicking the search button? I do not want to show an empty table. Below is the code snippet that I have tried: Howev ...

How to capture the "chip close" event in Vuetify

Exploring the vuetify realm as a newcomer, I find myself grappling with event handling while working on my first web app project. Specifically, I am currently developing a "UserPicker" component using VAutocomplete. This component functions by sending an ...

What is the solution to the TypeScript error stating that there is no index signature with a parameter of type 'string' on the 'Object' type?

While working on an Angular project, I came across an issue when writing a Typescript function for a service. The error message stated: "Element implicitly has an 'any' type because expression of type 'string' can't be used to inde ...

Using a variable as an argument for a DOM function in JavaScript

I found this code snippet on a website and made some changes to it. However, the modified code below is not functioning as expected. My goal was to hide the div with the id "demo1", but for some reason, it's not working. What could be causing this is ...

Adding various items to a group

I am currently working on generating sequential barcodes based on a specified quantity and storing them in a MongoDB database. To achieve this, I utilize the following code snippets: db.controlNumber.insert({controlNumber}) to insert the generated barcode ...

jQuery failing to transmit JSON in AJAX POST call

I'm a bit perplexed at the moment, as I'm attempting to send data to my Node.js server using the code snippet below: $.ajax({type:'POST', url:'/map', data:'type=line&geometry='+str, succe ...

React: Update the hidden input's value to reflect the total number of spans clicked

I have a component called Rating that displays a star rating like this: var Rating = React.createClass({ render: function () { return ( <div className="rate-line"> <div className="rate-title">{this.props ...

Comparing WebSockets and XHR for transmitting data

Looking to optimize the architecture of a web application using Node.js, the goal is to efficiently send medium-sized files to the client from a gallery. Each gallery item should be delivered to the user as quickly as possible in binary form. The file size ...

Obtaining Runtime Inputs from the Command Line

I have been using a unique Vue SPA boilerplate found at this link, which utilizes webpack as its foundation. During the development process or when deploying the application, I have successfully utilized process.env.NODE_ENV to differentiate between a dev ...

Encountering issues with Next JS navigation when using the mobile back button

Recently, I put together a website using Next js 13 (with app router) and utilized Mantine UI for the front-end design. On mobile devices, I have implemented a responsive menu on the homepage. However, when I try to navigate back to the homepage from the ...

Ways to stop the Kendo Panelbar from collapsing upon clicking a button on the panelbar

My challenge involves a button placed on a Kendo Panelbar. I attempted to create a jQuery function to prevent the panelbar from collapsing when the button is clicked, as well as to avoid any post-back action. Unfortunately, my code isn't working as ex ...

Transfer a single search result to a different webpage from a list of multiple results

I am working on a webpage called userLanding.jsp. When a user searches, the page displays multiple results. I have successfully checked the selected dynamic result, but now I need to find a way to transfer the data from the selected div to another page. D ...

Synchronize CSS Properties with JavaScript Event

Is there a more efficient way to synchronize two properties using JavaScript or JQuery? For instance, when I have an editable text field and I switch it to an input box upon double click, how can I ensure that the input box adjusts its size along with the ...