In this page it is presented a JavaScript function that can be used to get the values of the selected (checked) checkboxes added into a form. The function receives the object with that form, then, returns an Array with the value of each selected /checked checkbox into that <form>
- Here's the function:
// Returns an array with values of the selected (checked) checkboxes in "frm"
function getSelectedChbox(frm) {
 // JavaScript & jQuery Course - https://coursesweb.net/javascript/
  var selchbox = [];        // array that will store the value of selected checkboxes

  // gets all the input tags in frm, and their number
  var inpfields = frm.getElementsByTagName('input');
  var nr_inpfields = inpfields.length;

  // traverse the inpfields elements, and adds the value of selected (checked) checkbox in selchbox
  for(var i=0; i<nr_inpfields; i++) {
    if(inpfields[i].type == 'checkbox' && inpfields[i].checked == true) selchbox.push(inpfields[i].value);
  }

  return selchbox;
}
Example usage getSelectedChbox() function. When click on a button into a form with various checkboxes, open an alert window with the values of the selected checkboxes.
Check the web site programming language you know:
<form action="script.php" method="post">
<input type="checkbox" name="chb[]" value="html" />HTML<br/>
<input type="checkbox" name="chb[]" value="css" />CSS<br/>
<input type="checkbox" name="chb[]" value="javascript" />JavaScript<br/>
<input type="checkbox" name="chb[]" value="php" />php<br/>
<input type="checkbox" name="chb[]" value="python" />Python<br/>
<input type="checkbox" name="chb[]" value="net" />Net<br/>
<input type="button" value="Click" id="btntest" />
</form>

<script type="text/javascript"><!--
// Returns an array with values of the selected (checked) checkboxes in "frm"
function getSelectedChbox(frm) {
 // JavaScript & jQuery Course - https://coursesweb.net/javascript/
  var selchbox = [];        // array that will store the value of selected checkboxes

  // gets all the input tags in frm, and their number
  var inpfields = frm.getElementsByTagName('input');
  var nr_inpfields = inpfields.length;

  // traverse the inpfields elements, and adds the value of selected (checked) checkbox in selchbox
  for(var i=0; i<nr_inpfields; i++) {
    if(inpfields[i].type == 'checkbox' && inpfields[i].checked == true) selchbox.push(inpfields[i].value);
  }

  return selchbox;
}

  /* Test this function */
// When click on #btntest, alert the selected values
document.getElementById('btntest').onclick = function(){
  var selchb = getSelectedChbox(this.form);     // gets the array returned by getSelectedChbox()
  alert(selchb);
}
//-->
</script>
Results (select some checkboxes, then click the Click button):
Check the web site programming language you know:
HTML
CSS
JavaScript
php
Python
Net

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Get the value of the selected /checked checkboxes in a form

Last accessed pages

  1. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (41724)
  2. Get Duration of Audio /Video file before Upload (5913)
  3. Follow the mouse cursor with a DIV inside a Parent (2703)
  4. Register and show online users and visitors (23278)
  5. Node.js Move and Copy Directory (1127)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (390)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (332)
  3. Register and show online users and visitors (302)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (234)
  5. Insert, Select and Update NULL value in MySQL (223)