Solved

Radio button Issue -

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

623 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