Solved

Radio button Issue -

Posted on 2004-09-01
6
275 Views
Last Modified: 2011-09-20
Hello,
 I have a radio button which is dynamically generated. There are some situvations that there will be only one radio button, on those situvations i am not able to get the value of the selected button in javascript also in asp.
Example,


<html>
<head><title>Test</title>
<script language="javascript">
function check(){
alert("Form is submitted");
alert("The value is"+document.form.rad1[0].value);
}
</script></head>
<body><form name="form">
<input type="radio" name="rad1" value='0'>
<input type="submit" name="submit" onclick="check();">
</form>
</body>
</html>

Please give your suggestions.

Thanks
-P
0
Comment
Question by:spcraja
6 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 11958810
Try:
If (document.form.rad1.length>0)
alert("The value is"+document.form.rad1[0].value);
else
alert("the value is+document.form.rad1.value);


Cd&
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11958967
Radio buttons do not work like select boxes or Checkboxes.

say you have two different radiobuttons

<input type="radio" name="rad1" value="1">
<input type="radio" name="rad2" value="2">

then their values can be recieed by

document.formemail.rad1.value
and
document.formemail.rad2.value

If you have multiple radio buttons with same name, say:

<input type="radio" name="commonrad" value="1">
<input type="radio" name="commonrad" value="2">

now this has become an array.

so then you have to run a loop to get the value..

i.e document.formemail.commonrad[0].value will give 1
and document.formemail.commonrad[1].value will give 2
0
 

Author Comment

by:spcraja
ID: 11958971
Actually i tried this before itself but is there is no way we can get this in an array, since when i need to handle this in ASP there i need to change the entire logic itself.

FYI - In your code If (document.form.rad1.length>0) should be If (eval(document.form.rad1.length)>0) Then only it will get the length.

For right now, I overcome this situvation by creating a dummy button when ever the length is 1, so still i have the values in array.

0
 
LVL 5

Accepted Solution

by:
KarcOrigin earned 100 total points
ID: 11959415
Hi,
If you do not want to change your code and still want your radio button as an array then do like this.

Just an example :

<html>
  <body>
    <form name="test" method="post">
      <input type="hidden" name="rad1" value="0">
      <!-- Here you generate your other radio buttons -->
    </form>
  </body>
</html>

In this case you will always be having at least 2 objects (one is hidden other is radio button with the same name) and it will act like an array. Remember since you always have this hidden object with the same name as your radio button start your processing with index 1 (because index 0 is always dummy hidden variable).

I hope it will help you in some way.

Cheers!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question