Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Radio button Issue -

Posted on 2004-09-01
6
Medium Priority
?
312 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
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.
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 …
Introduction to Processes

885 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