I use a checkbox of checkboxes to capture multiple selections
The following code with two flags works well and returns 2 as expected (or, however much).
However, if there is only one flag element in the array, it returns a length of 0 (zero) .... why is this? Shouldn't it return a length of 1?
I tried this in Internet Explorer and Chrome with the same results. While working, I have to turn on the hidden fake flag in the array to make sure that there are always two or more elements during code execution.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <script language="javascript" type="text/javascript"> function categoryOnClick() { var selectedRows = document.searchForm.elements['categorySelect[]']; alert(selectedRows.length); } </script> </head> <body> <form name="searchForm" action=""> <input type="checkbox" name="categorySelect[]" id="1" onclick="categoryOnClick();"/> <input type="checkbox" name="categorySelect[]" id="2" onclick="categoryOnClick();"/> </form> </body> </html>
Code that returns 0 (zero) length ...
<form name="searchForm" action=""> <input type="checkbox" name="categorySelect[]" id="1" onclick="categoryOnClick();"/> </form>
source share