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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.