PDA

View Full Version : Another Simple Problem


scriptkeeper
08-08-2003, 09:59 PM
Another simple problem I should not have troublle with! I need to check to see if atleast one checkbox is checked! I cant understand what the problem could be? I get an object expected error on line 24!


<html>
<head>
<script>
function checkThem(which){
var form=document.forms[which];
for(var i=0;i<form.elements.length;i++){
if(form.elements[i].checked){
\\ statments/functions to execute upon finding checked box
window.alert("you have checked "+form.elements[i].name);
}else{
\\ statments/functions to execute upon finding unchecked boxes
window.alert("you hav checked no boxes");
}}
}
</script>
</head>
<body bgcolor="#FFFFFF">
<form name="check">
<input type="checkbox" name="check1">
<input type="checkbox" name="check2">
<input type="checkbox" name="check3">
<input type="checkbox" name="check4">
<input type="checkbox" name="check5">
<input type="button" value="Check Me" onclick="checkThem('check')">
</form>
</body>
</html>

scriptkeeper
08-08-2003, 10:14 PM
Sorry I figured it out was my stupid comments duh! Any one else notice my stupidity?

Roy Sinclair
08-08-2003, 10:15 PM
Use "//" instead of "\\" for comments, one works while the other just makes your script fail to run. Once that's fixed you'll note that you get an alert for each and every form element including the submit button. You need to add a counter to track how many elements are checked and then display an alert. The spelling problems are understandable for a quick test page so...

Edit: Turn on the active reporting of javascript errors in your browser and you'll get notified of errors like the wrong characters used for comments right away. Obviously you missed that notice and spent time looking for a simple solution.

scriptkeeper
08-08-2003, 10:23 PM
Thanx Roy your always so helpfull!

Yeeeaahhh!! It Works!


<html>
<head>
<script>
function checkThem(which,ctr){
var form=document.forms[which];
for(var i=0;i<ctr;i++){
if(form.elements[i].checked){
alert("you have checked "+form.elements[i].name);
break;
}else{
alert("you hav checked no boxes");
break
}}
}
</script>
</head>
<body bgcolor="#FFFFFF">
<form name="check">
<input type="checkbox" name="check1">
<input type="checkbox" name="check2">
<input type="checkbox" name="check3">
<input type="checkbox" name="check4">
<input type="checkbox" name="check5">
<input type="button" value="Check Me" onclick="checkThem(this.form.name,5)">
</form>
</body>
</html>