[javascript] Getting all selected checkboxes in an array

ES6 version:

const values = Array
  .from(document.querySelectorAll('input[type="checkbox"]'))
  .filter((checkbox) => checkbox.checked)
  .map((checkbox) => checkbox.value);

_x000D_
_x000D_
function getCheckedValues() {_x000D_
  return Array.from(document.querySelectorAll('input[type="checkbox"]'))_x000D_
  .filter((checkbox) => checkbox.checked)_x000D_
  .map((checkbox) => checkbox.value);_x000D_
}_x000D_
_x000D_
const resultEl = document.getElementById('result');_x000D_
_x000D_
document.getElementById('showResult').addEventListener('click', () => {_x000D_
  resultEl.innerHTML = getCheckedValues();_x000D_
});
_x000D_
<input type="checkbox" name="type" value="1" />1_x000D_
<input type="checkbox" name="type" value="2" />2_x000D_
<input type="checkbox" name="type" value="3" />3_x000D_
<input type="checkbox" name="type" value="4" />4_x000D_
<input type="checkbox" name="type" value="5" />5_x000D_
_x000D_
<br><br>_x000D_
<button id="showResult">Show checked values</button>_x000D_
<br><br>_x000D_
<div id="result"></div>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios

Examples related to dhtml

Child element click event trigger the parent click event Change :hover CSS properties with JavaScript Modifying a query string without reloading the page Adding and removing style attribute from div with jquery Set content of HTML <span> with Javascript jQuery get the id/value of <li> element after click function Capture iframe load complete event How do I attach events to dynamic HTML elements with jQuery? How do I programmatically click on an element in JavaScript? Getting all selected checkboxes in an array