javascript - Get Selected check box items from a click of a button -


how can go getting checked check box item's id in repeated list click of button , add items variable / array later use?

html:

<input id="btncheck" type="button" value="next" ng-click="addselected()" />     <div ng-controller="moviecontroller">         <ul>             <li ng-repeat="movie in movies">                     <input id="chkbox-{{ movie.movieid }}"                          type="checkbox"                          ng-checked="selection.indexof(movie.movieid) > -1"                          ng-click="toggleselection(movie.movieid)"                     />             </li>         </ul>     </div> 

script:

$scope.addselected = function () {     var selected = $scope.selection     console.log(selected); }  $scope.selection = []; $scope.toggleselection = function toggleselection(movie) {     var idx = $scope.selection.indexof(movie);     if (idx > -1) {         $scope.selection.splice(idx, 1);     }     else {         $scope.selection.push(movie);     } }; 

you create custom filter return selected value id, need pass 2 parameters filter 1st parameter perform check, if true 2nd parameter property array return filter.

markup

<body ng-app="app">   <div ng-controller="moviecontroller">     {{(movies | returnpropertywhencheckedtrue: 'checked' :'movieid')}}     <ul>       <li ng-repeat="m in movies">         <input id="chkbox-{{ m.movieid }}" type="checkbox" ng-model="m.checked"/>       </li>     </ul>   </div> </body> 

filter

app.filter('returnpropertywhencheckedtrue', function() {   return function(array, propertytochecked, property) {     var returnarray = [];     angular.foreach(array, function(value, index) {       if (value[propertytochecked])         returnarray.push(value[property]);     });     return returnarray;   } }); 

working plunkr


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -