Javascript and only one Checkbox - undefined

25,095

Solution 1

Use a loop control variable, and set it to 1 if length is undefined...

var len = document.checks.user.length;
if(len == undefined) len = 1;
for (i = 0; i < len; i++) //for all check boxes

Best regards...

Solution 2

if (document.getElementById('Checkbox1').checked) { /* do something */ }

if you want to loop a bunch of checkboxes, you could loop the input fields of your form, like:

var formNodes  = document.checks.getElementsByTagName('input');
for (var i=0;i<formNodes.length;i++) {
   /* do something with the name/value/id or checked-state of formNodes[i] */
}

Solution 3

I too face the same proble with total of 5 checkboxes out of which 4 are disabled and 1 is enabled. Now the checkboxId.length is undefined, so the only option I could think of is if(checkboxId.length ==undefined ){checkboxId.checked=true & submith the form}.

Solution 4

if(document.checks.user[0]) {
  //it's an array
}
else {
  //it's a single element
}

Solution 5

It's very simple, just create a hidden input tag with the name same as the existing checkbox input tag.

For example:

<input type="checkbox" name="user" value="Bike" ID="Checkbox1">
<input type="hidden" name="user" value=""/>
Share:
25,095
T.T.T.
Author by

T.T.T.

Updated on December 28, 2020

Comments

  • T.T.T.
    T.T.T. over 3 years
    for (i = 0; i < document.checks.user.length; i++) //for all check boxes
    {
        if (document.checks.user[i].checked == true )
        {
            document.checks.submit();
            return 0;
        }
    }
    
    <body>
    <form action="" method=POST name="checks" ID="Form2">
      I have a bike:
      <input type="checkbox" name="user" value="Bike" ID="Checkbox1">
      <br>
      <br>
    </form>
    <input type="button" value="Delete" 
        class="btn" onclick="sub_delete()" 
        onmouseover="hov(this, 'btn btnhov')" onmouseout="hov(this, 'btn')" 
        id="Button1" name="Button1" 
    />
    </body>
    

    as you probably already know when there is only one check box left document.checks.user.length = undefined. Whats the most efficient way to make sure that when there is only one check box, it will be deleted. I was thinking just thinking to add it as a seperate if statement before the if statement here.....any suggesstions.

    Thanks.

  • jishi
    jishi over 15 years
    checks is the name of the <form>, user is the name of the checkbox.
  • chaos
    chaos over 15 years
    Ugh, recommending a massive framework for the simplest of tasks.
  • teedyay
    teedyay over 15 years
    Heh - yeah, I take your point. I guess I'm just a bit wowed by jQuery of late: it's solved so many of my browser-specific headaches! :-)
  • T.T.T.
    T.T.T. over 15 years
    This is what I am looking for. However, I realized when there is only one element left, the if statement I have will not work when there is only 1 element. i.e -> if (document.checks.user[0].checked == true )
  • T.T.T.
    T.T.T. over 15 years
    I believe it no longer sees user as an array....? I'll put the document.checks.submit(); in this loop control.