Language: Javascript
I really hate to ask such a seemingly simple question, but simple as it seems, I can't get this to work.
I am trying to do this entirely with pure Javascript alone (no library support).
I have a form with checkboxes...
All checkboxes are named files[] because I use the results in an array:
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
What I'm trying to do is, when the user submits the form:
- IF no checkbox is checked >> return ALERT!
- ELSE submit the form
Here's my form:
<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
<input type="submit" value="Submit" name="submit">
</form>
And here's my Javascript code:
function confirm_update() {
var aCheckbox = document.deleteFiles.getElementsByTagName('input');
if (aCheckbox.checked){
return confirm("Are you sure you want to proceed deleting the selected files?");
} else {
alert("You do not have any selected files to delete.");
return false;
}
}
In action: http://jsfiddle.net/DVqwB/3/
Apparently, it is not working, I know I should use getElementsById but since they each have unique IDs I can't use that. And I also know that there are lots of solutions on this site, but if you look - they actually use jQuery...
Any help & guidance would be greatly appreciated! Thank you so much.