Jump to content

checkbox validation


nadeemshafi9

Recommended Posts

hi guys

 

i am trying to get the checkbox.length so i can loop throgh and check if checked but it says the length is |UNDEFINED when i output it in a alert i tried size and it says 0 but there ar three check boxes in my name="zone_type[]"

 

document.getElementById("zone_type[]").length returns undefined

 

 

plz help

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/110908-checkbox-validation/
Share on other sites

You have set the "names" to be an array, but you are trying to grab that array using getElementById(). And, you can't give multiple elements the same ID anyway. Instead reference the collection by their name as an element of the form:

 

<html>
<head>
  <script type="text/javascript">

    function countBoxes() {
      alert(document.forms[0]['zone_type[]'].length);
    }

  </script>
</head>

<body>
  <form>
    Check 1 <input type="checkbox" name="zone_type[]" value="1"><br>
    Check 2 <input type="checkbox" name="zone_type[]" value="2"><br>
    Check 3 <input type="checkbox" name="zone_type[]" value="3"><br>
    <button onclick="countBoxes();">Check</button>
  </form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/110908-checkbox-validation/#findComment-569115
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.