Check all/Uncheck all checkboxes in javascript

In this tutorial we will learn how to check all and uncheck all checkboxes using javascript. Actually we want to check all and uncheck all checkboxes in a php/asp webpage, as you already know php/asp webpage access the form field using its name attribute, so in this tutorial we will check all and uncheck all checkboxes using the id attribute of the checkbox so that you doesn't feel any difficulty to send form checkboxes data from one page to another. Let’s have a look

<html>

<head>

<title> Check all/Uncheck all checkboxes in javascript for php webpages</title>

<script language="JavaScript" type="text/javascript">
function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}
function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
</script>
</head>
<body>
<form name="myform" action="checkboxes.asp" method="post">

<input type="checkbox" id="check_list" name="chk_Asp" value="1">ASP<br>
<input type="checkbox" id="check_list" name="chk_Php" value="2">PHP<br>
<input type="checkbox" id="check_list" name="chk_Javascript" value="3">JavaScript<br>
<input type="checkbox" id="check_list" name="chk_Coldfusion" value="4">Cold Fusion<br>
<input type="checkbox" id="check_list" name="chk_Jquery" value="5">JQuery<br>

<input type="button" name="Check_All" value="Check All" onclick="CheckAll(document.myform.check_list);">

<input type="button" name="Un_CheckAll" value="Uncheck All" onclick="UnCheckAll(document.myform.check_list);">

</body>

If you want to
check all and uncheck all checkboxes by clicking hyperlink then use this

<a href="javascript:CheckAll(document.myform.check_list)"> Check All </a>
<a href="javascript:UnCheckAll(document.myform.check_list)">Un Check All</a>

So this is the way you can
check all and uncheck all checkboxes in javascript for php/asp webpages.

0 comments: