Solved

Radio button Issue -

Posted on 2004-09-01
6
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …

749 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