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 - http://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 - http://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 tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Get the value of the selected /checked checkboxes in a form

Last accessed pages

  1. Dog Mahjong (6631)
  2. Mahjong Games (19943)
  3. Colors In The Sky 2 (1496)
  4. Farm Frenzy2 (384)
  5. Editing, Changing XML - E4X (1061)

Popular pages this month

  1. Flash Games - Free online Games (1360)
  2. Qwop (1158)
  3. Tunnel Rush (1062)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (983)
  5. Drag Racer V3 (758)