We help IT Professionals succeed at work.

checkboxes - vbscript or javascript

Panoscha32
Panoscha32 asked
on
hello there! I have in an ASP file the following checkboxes:

<input type="checkbox">Choice 1<br>
<input type="checkbox">Choice 2<br>
<input type="checkbox">Choice 3<br>
<p>
<input type="checkbox">Checked<br>
<input type="checkbox">Unchecked
</p>

I want each time I select the 'Checked' checkbox, Choice1, Choise2 and Choice3 boxes to be selected. And each time I select 'Unchecked' the three previous to be unselected.
Comment
Watch Question

This is Javascript, not ASP (just for the record!)...

<input type="checkbox" NAME="Choice1">Choice 1<br>
<input type="checkbox" NAME="Choice2">Choice 2<br>
<input type="checkbox" NAME="Choice3">Choice 3<br>
<p>
<input type="checkbox" NAME="AllCheck" onClick="CheckAll();">Checked<br>
<input type="checkbox" NAME="AllUnCheck" onClick="UnCheckAll();">Unchecked
</p>

<script language="javascript">
<!--
function CheckAll() {
    document.forms[0].Choice1.checked = true;
    document.forms[0].Choice2.checked = true;
    document.forms[0].Choice3.checked = true;
}

function UnCheckAll() {
    document.forms[0].Choice1.checked = false;
    document.forms[0].Choice2.checked = false;
    document.forms[0].Choice3.checked = false;
}
//-->
</script>
Or, if you had more Choice's to check, you could try a simple loop:


function CheckAll() {
   for(i=1; i<4; i++) {
      eval('document.forms[0].Choice' + i + '.checked = true');
   }
}

Author

Commented:
Looks good but for some reason it doesn't work. I receive at the buttom of my browser this annoying message 'Error on page'.

Author

Commented:
OK i found it. Thanks very much. The only ommission was that:

<form method="post"><form>

inside <body> section. So here it goes:

<html>
<head>
<title>some title</title>

<script language="javascript">
<!--
function CheckAll() {
   document.forms[0].Choice1.checked = true;
   document.forms[0].Choice2.checked = true;
   document.forms[0].Choice3.checked = true;
}

function UnCheckAll() {
   document.forms[0].Choice1.checked = false;
   document.forms[0].Choice2.checked = false;
   document.forms[0].Choice3.checked = false;
}
//-->
</script>

</head>

<body>
<form method="post">
<input type="checkbox" NAME="Choice1">Choice 1<br>
<input type="checkbox" NAME="Choice2">Choice 2<br>
<input type="checkbox" NAME="Choice3">Choice 3<br>
<p>
<input type="checkbox" NAME="AllCheck" onClick="CheckAll();">Checked<br>
<input type="checkbox" NAME="AllUnCheck" onClick="UnCheckAll();">Unchecked
</p>
</form>
<body>

</html>

Explore More ContentExplore courses, solutions, and other research materials related to this topic.