Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple selected check box values

How to get multiple selected check box values in Angular.js and insert into database separated with a comma (,) ?

like image 385
user15009 Avatar asked Dec 13 '14 09:12

user15009


People also ask

How do I get multiple checkbox values?

Using jQuery, we first set an onclick event on the button after the document is loaded. In the onclick event, we created a function in which we first declared an array named arr. After that, we used a query selector to select all the selected checkboxes. Finally, we print all the vales in an alert box.

Can checkbox select multiple values?

A checkbox is also known as a tick box or a selection box. A checkbox is a tiny user interface component that allows users to select more than one option from a given options list. We will create a small fruits list using HTML 5 and display that checkboxes list to the users to select more than one option.

How to select multiple check boxes in jQuery?

Here to select multiple check boxes we selected each check box one by one. Select Element by id using document.getElementById (). then check checkbox is checked or not. If checked then push in array. JQuery makes code simple and easy, to select checkbox we can use either name, id or input type checkbox. We can use any of above ways as per need.

How to get multiple checkbox value in JavaScript with getElementById?

How to get multiple checkbox value in javascript with getElementById () Here to select multiple check boxes we selected each check box one by one. Select Element by id using document.getElementById (). then check checkbox is checked or not.

How to get the value of all checkboxes in a form?

You will now see how to get the value of all checkboxes using the querySelectorAll () method marked by the user. This will fetch the checkboxes values from the HTML form and display the result. Now, you will see how to get all checkbox values marked by the user.

How many checkboxes can be used in an application?

In your application you can use single check box or you can use multiple checkbox . Below examples will help you to understand multiple checkbox validation in j ...


1 Answers

You can use ng-repeat + ng-model over an array of options.

DEMO http://plnkr.co/edit/KcUshc74FZL1npZsKbEO?p=preview

html

<body ng-controller="MainCtrl">
  <div class="container">

    <h1>Options</h1>

    <div>

      <div class="form-group" ng-repeat="option in options">
        <label>{{option.name}}</label>
        <input type="checkbox" ng-model="option.value">
      </div>

    </div>

    <hr>
    <button class="btn btn-primary" ng-click="save()">save to database</button>

  </div>


</body>

js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

  $scope.options = [{
    name: 'java',
    value: true,
  }, {
    name: 'c#',
    value: false
  }, {
    name: 'angular',
    value: true
  }, {
    name: 'r',
    value: false
  }, {
    name: 'python',
    value: true
  }, {
    name: 'c++',
    value: true
  }];

  $scope.save = function() {

    var optionsCSV = '';

    $scope.options.forEach(function(option) {

      if (option.value) {

        // If this is not the first item
        if (optionsCSV) {
          optionsCSV += ','
        }
        optionsCSV += option.name;
      }

    })

    // Save the csv to your db (replace alert with your code)
    alert(optionsCSV);

  };


});
like image 134
Jossef Harush Kadouri Avatar answered Oct 11 '22 11:10

Jossef Harush Kadouri