Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript and only one Checkbox - undefined

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.

like image 815
T.T.T. Avatar asked Feb 10 '09 23:02

T.T.T.


4 Answers

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

like image 98
Josh Stodola Avatar answered Oct 01 '22 07:10

Josh Stodola


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] */
}
like image 31
KooiInc Avatar answered Oct 01 '22 05:10

KooiInc


if(document.checks.user[0]) {
  //it's an array
}
else {
  //it's a single element
}
like image 22
Diodeus - James MacFarlane Avatar answered Oct 01 '22 05:10

Diodeus - James MacFarlane


Your question is somewhat confusing, since your javascript would obviously have to be inside a function called 'sub_delete' to be any use... someone with the mighty power to edit questions might improve the question by making that clearer...

So the first issue you have to get around is the fact that for single checkbox, with a given name 'user', is not an array, and therefore has no defined length, but also if you try and access it as an array.. things get confused.. a full rewrite of your javascript function might look like this:

    function sub_delete{
        if (typeof document.checks.user.length === 'undefined') {
   /*then there is just one checkbox with the name 'user' no array*/
        if (document.checks.user.checked == true )
                            {
                                document.checks.submit();
                                return 0;
                            }   
    }else{
  /*then there is several checkboxs with the name 'user' making an array*/
        for(var i = 0, max = document.checks.user.length; i < max; i++){
            if (document.checks.user[i].checked == true )
                            {
                                document.checks.submit();
                                return 0;
                            }

        }
    }
    }//sub_delete end

HTH, -FT

like image 27
ftrotter Avatar answered Oct 01 '22 07:10

ftrotter