Solved

Radio button Issue -

Posted on 2004-09-01
6
242 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now