Archive for JavaScript

JavaScript – Select Checkbox

Thursday, February 26th, 2009

Most of time we spend time on client side before sending request to the server.
We use checkbox in form for user friendly environment. If user wants to select checkbox, JavaScript provide us functions. This function easily selects checkbox through JavaScript. This can also uncheck through JavaScript.

document.frm.id1.checked="true"

The example we are providing to select checkbox. This example use the function of “checked” and true or false, depends on the select or unselect. The true value makes checkbox selected and false value do checkbox unselected

Demo

<html>
<head>
<title>JavaScript select checkbox</title>
<script>
function selectCheckBox()
{
  var e= document.frm.elements.length;
  var cnt=0;

  for(cnt=0;cnt<e;cnt++)
  {
    if(document.frm.elements[cnt].name=="id"){
     document.frm.elements[cnt].checked="true"
    }
  }
}
</script>
</head>

<body>
<form name="frm">
<input type="checkbox" name="id" value="1">
<input type="checkbox" name="id" value="2">
<input type="checkbox" name="id" value="3">
<input type="checkbox" name="id" value="4">
<input type="checkbox" name="id" value="5">
<input type="button" name="goto" onClick="selectCheckBox()" value="Check">
</form>
</body>
</html>

The example is showing how to select checkbox in JavaScript.

<html>
<head>
<title>JavaScript select checkbox</title>
<script>
function selectCheckBox()
{
     document.frm.id1.checked="true"
     document.frm.id2.checked="true"
     document.frm.id3.checked="true"
}
</script>
</head>

<body>
<form name="frm">
<input type="checkbox" name="id1" value="1">
<input type="checkbox" name="id2" value="2">
<input type="checkbox" name="id3" value="3">

<input type="button" name="goto" onClick="selectCheckBox()" value="Check">
</form>
</body>
</html>