Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

which.element.length undefined for length 1? 1

Status
Not open for further replies.

jmoponfire

Programmer
Oct 1, 2004
3
US
If I have a form, with one checkbox, why does the length attribute return a value of undefined? Also, why can't I access the zero'th element in a single checkbox as in:
if(which.Question_1[0].checked)?

To see how this is causing a problem for me:
or
Code:
<html>
<head>
<script>
function checkQuestion_1(which){
        myOption = -1;
        for (i=0; i<which.Question_1.length; i++) {
                if (which.Question_1[i].checked) {
                        myOption = i;
                }
        }
        if (myOption == -1) {
                alert("You must agree to the terms before continuing");
                return false;
        }
        else
                return true;
};
</script>
</head>
<form onsubmit='return (checkQuestion_1(this))'>
<body>
I Agree to the terms: 
<input type=checkbox name='Question_1' value='1'>
<input type=submit name=submit value='Continue'>
[\code]


If I have two or more checkboxes this function works fine. 

I understand there may be an entirely different approach to do this, but I am working with an application that dynamically generates the j.s. so I'd like to find the most simple fix possible, as this simple smidge of code comes from something more involved. I've just traced it to this simple problem. 

Thanks for any help.
 
I've not tried it, but try replacing this:

Code:
for (i=0; i<which.Question_1.length; i++) {
   if (which.Question_1[i].checked) {
      myOption = i;
   }
}

with this:

Code:
if (which.Question_1.length) {
	for (i=0; i<which.Question_1.length; i++) {
	   if (which.Question_1[i].checked) {
		  myOption = i;
	   }
	}
} else {
   myOption = which.Question_1.selectedIndex;;
}

AFAIK, you cannot query the length property because a single element is not classed as a collection, and therefore has no need for a length (it would always be 1).

Hope this helps,
Dan
 
also, you should avoid using "which" as a var name, since it is reserved in NS/Moz for mouse buttons (IIRC)...


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Thanks for the suggestion, but the outcome is the same.
which.Question_1.selectedIndex; is undefined whether the
box is checked or not.

Comment on which as a var name is noted also, thanks.

 
Why are you using a collection of checkboxes with the same name? Radio buttons or selects are better for selecting only one. In most circumstances, you would name the checkboxes Question_1, Question_2, etc.

Back to your question:
Try changing <input type=checkbox name='Question_1' value='1'>
to <input type=checkbox name='Question_1[]' value='1'>

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
I often use collections of checkboxes with the same name. Also, I would not put [] in the names of your HTML elements unless you ABSOLUTELY have to (I believe that many PHP developers do this to create arrays). This will cause problems with the rest of your code where you are referencing elements by document.formname.elementname because the [] at the end is reserved as javascript syntax for Javascript arrays. The only way to access an element (from what I've found) that is declared this way is by document.formname.elements['elementname[]'].

Anyway, to your original question, Dan was correct in his assumption that your code is breaking because you're trying to access an array of checkboxes when only one checkbox exists. Since the number corresponding to the checkbox is irrelevant in your code (you're not returning the index of the checked item, so that info will be ignored), I would rewrite your function like so:
Code:
function checkQuestion_1(which){
   if (which.Question_1.length) {
      for (i = 0; i < which.Question_1.length; i++) {
         if (which.Question_1[i].checked) {
            return true;
         }
      }
   }
   else {
      if (which.Question_1.checked) {
         return true;
      }
   }
   //don't need an if here because if you got this far nothing was checked
   alert("You must agree to the terms before continuing");
   return false;
}

-kaht

banghead.gif
 
Thanks chessbot. I'm using the same name for an arbritray number of multiple checkboxes because it is less complex in PHP to create my array of values after submit. I'm not really interested in the field names, just the values selected for a particular question.

I'm dynamically creating forms/corresponding j.s. and in the case where the user creates a checkbox form element with only one value, my dynamic j.s. broke. I'm still learning the ins and outs of the dom by trial and error.

Kaht, thanks for the comments. Your solution works, and will be easiest to implement into what I've already got going,
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top