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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird radio button count error

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
I have a radio button

<input type="radio" name="shipadd" value="1" id="shipadd" />

When I do this -


alert(document.thisform.shipadd.length); I get my value undefined.

But when I have 2 radio buttons with the same name I get value as 2.

Why is this happening? Shouldn't the value with one radio button be 1.

Because the value is "undefined" a simple for loop doesn't work for me.

for (counter = 0; counter < document.thisform.shipadd.length; counter++)

Any help would be great.Thanks guys.
 
Radio buttons that do not exist in pairs (i.e. only one radio button exists with a particular name) are not treated as an array of elements - and as such do not have length properties and trying to access that property will return null. So, you will have to modify your function to detect for this. Here's an example:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {

   alertValues(document.forms["theForm"].elements["radio1"]);
   alertValues(document.forms["theForm"].elements["radio2"]);
 
};

function alertValues(obj) {
   if (obj.length) {
      for (var i = 0; i < obj.length; i++) {
         alert(obj[i].value);
      }
   }
   else {
      alert(obj.value);
   }
}

</script>
<style type="text/css"></style>
</head>
<body>

<form id="theForm">
   <input type="radio" name="radio1" value="radio1-1" />
   
   <input type="radio" name="radio2" value="radio2-1" />
   <input type="radio" name="radio2" value="radio2-2" />
</form>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
That's because [tt]document.thisform.shipadd[/tt] refers to a collection when there is more than one radio button with the same name, but when there is only one button it refers to only that button, not a collection. Use [tt]document.getElementsByName("myRadioGroup")[/tt] to always return a collection.

Adam
 
I added a hidden field with the same name and it seemed to work. Is that a good fix?
 
Is that a good fix?

Not really. You're adding useless markup that serves no purpose to your page when you can solve the problem with 2 or 3 lines of javascript. Did you read my post above?

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top