Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Using radio buttons with JavaScript

This tutorial will show how to make a textbox display a text depending on which radio button was checked.

On Monday, September 4th 2006 at 02:05 PM
By Sorin Sodolescu (View Profile)
*****   (Rated 4.1 with 109 votes)
Contextual Ads
More JavaScript Resources
Advertisement

In this tutorial we will see how to make a textbox display a text, depending on what radio button is checked.
Let's say that you have the following form, containing radio buttons, a button, used to call the JavaScript function, and a textbox:

<html>
   <body>
      Vote for your favourite sport:
      <form name="myform">
         <input type="radio" name="sports" value="Fotball" />Football<br />
         <input type="radio" name="sports" value="Hockey" />Hockey<br />
         <input type="radio" name="sports" value="F1" />F1<br />
         <input type="radio" name="sports" value="Basketball" />Bascketball<br />
         <input type="radio" name="sports" value="Tennis" />Tennis<br />
         <input type="radio" name="sports" value="Golf" />Golf<br />
         <input type="button" value="VOTE" onClick="vote()" /><br>
         <input type="text" id="txt" />
      </form>
   </body>
</html>


In order to display the chosen sport in the textbox, we will have to loop through the form to find out what radio button was checked. To do this we will use a variable, i, with the "for" statement.

<head>
   <script type="text/javascript">
   <!--
   function vote()
   {
      var t="Your favourite sport is: "
      for (i=0; i<document.myform.sports.length; i++)


The initial value of i is 0, because the first index of the form is 0. The final value is document.myform.sports.length, basically the last index of the form.
The next part checks if a radio button was checked, starting with the first radio button, index 0 (i=0).

      {
         if (document.myform.sports[i].checked==true)
         {
            t =t +document.myform.sports[i].value
         }
      }

If a radio button was checked then the t variable will become "Your favourite sport is " + the radio button's value. If not, t will remain the same.
This part displays the t variable in the textbox.

      document.getElementById("txt").value=t
   }
   -->
   </script>
</head>


But there is the possibility of the function being called without clicking a radio button. In that case the textbox will read "Your favourite sport is ". We can avoid this situation by using an if statement after the for loop, replacing the "document.getElementById("txt").value=t" line above with:

if (t=="Your favourite sport is: ")
{
   document.getElementById("txt").value="Choose a sport"
}
else
{
   document.getElementById("txt").value=t
}


After doing so, if the function is called without clicking a radio button first, the textbox will read "Choose a sport".
Finally, i've put the function into a HTML comment tag, <code><html> <!-- --> </html></code>, to keep old browsers that can't recognize the script from displaying it.
Here is the entire code, that you can paste into a HTML document:

<html>
   <head>
      <script type="text/javascript">
      <!--
      function vote()
      {
         var t="Your favourite sport is: "
         for (i=0; i<document.myform.sports.length; i++)
         {
            if (document.myform.sports[i].checked==true)
            {
               t =t +document.myform.sports[i].value
            }
         }
         if (t=="Your favourite sport is: ")
         {
            document.getElementById("txt").value="Choose a sport"
         }
         else
         {
            document.getElementById("txt").value=t
         }
      }
      -->
      </script>
   </head>
   <body>
      Vote for your favourite sport:
      <form name="myform">
         <input type="radio" name="sports" value="Fotball" />Football<br />
         <input type="radio" name="sports" value="Hockey" />Hockey<br />
         <input type="radio" name="sports" value="F1" />F1<br />
         <input type="radio" name="sports" value="Basketball" />Bascketball<br />
         <input type="radio" name="sports" value="Tennis" />Tennis<br />
         <input type="radio" name="sports" value="Golf" />Golf<br />
         <input type="button" value="VOTE" onClick="vote()" /><br>
         <input type="text" id="txt" />
      </form>
   </body>
</html>

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Blippo on Sunday, January 7th 2007 at 05:34 PM

So far, I have no idea on how to read radiobutton values in Greasemonkey scripts.
I really would like to see a version of the script that is compatible with Firefox\'s XPCNativeWrappers.
Anyone have some good ideas?

by KOOchi on Wednesday, September 12th 2007 at 08:48 AM

gochii

by WashingTon on Wednesday, September 12th 2007 at 08:49 AM

by Kadrus on Saturday, September 15th 2007 at 05:31 AM

I did it in an other way...but still the same result

<html>
<head>
<script type="text/javascript">
function check(sport)
{
document.getElementById("answer").value=sport
}
</script>
</head>
<body>

<p>What's your favorite sport?</p>

<form>
<input type="radio" name="sport" onclick="check(this.value)" value="Basketball">Basketball<br />
<input type="radio" name="sport" onclick="check(this.value)" value="Football">Football<br />
<input type="radio" name="sport" onclick="check(this.value)" value="Baseball">Baseball<br />
<input type="radio" name="sport" onclick="check(this.value)" value="Soccer">Soccer<br />
<br />
Your favorite sport is: <input type="text" id="answer" size="20">
</form>

</body>
</html>

by Sorin Sodolescu on Saturday, September 15th 2007 at 06:09 AM

Hi Kadrus

Yes, putting the "Your favourite sport is: " text in the body and calling the function by clicking the radio button saved you from writing some extra code. That's great! But the point of this tutorial was to introduce beginners to for loops, arrays and obviously radio buttons. Nevertheless, congratulations for writing shorter, more efficient code!

by sdfsdf on Friday, June 20th 2008 at 04:16 AM

fdbgdbgdf

by fcdasdas on Tuesday, September 23rd 2008 at 01:22 AM

dsfdsfds

by Rana on Monday, November 17th 2008 at 02:57 AM

We can use this for other validation
<script type="text/javascript">
<!--
function validForm()
{
var t="check"
for (i=0; i<document.myform.sports.length; i )
{
if (document.myform.sports[i].checked==true)
{
t =t document.myform.sports[i].value
}
}

if (t=="check"){
alert("You should fill all fields properly");
return false;
}
else {
return true;
}
}
-->
</script>

<form name="myform">
<input type="radio" name="sports" value="Fotball" />Football<br />
<input type="radio" name="sports" value="Hockey" />Hockey<br />
<input type="radio" name="sports" value="F1" />F1<br />
<input type="radio" name="sports" value="Basketball" />Bascketball<br />
<input type="radio" name="sports" value="Tennis" />Tennis<br />
<input type="radio" name="sports" value="Golf" />Golf<br />
<input type="button" value="VOTE" onClick="validForm()" /><br>
</form>

by Chetan on Tuesday, November 25th 2008 at 01:36 AM

Thanx Kadrus!!!!

by Chetan on Tuesday, November 25th 2008 at 01:36 AM

Thanx Kadrus!!!!

by hi on Wednesday, January 28th 2009 at 06:59 AM

wst site very poor site

by vrushali on Wednesday, February 25th 2009 at 05:46 AM

Hi...
i have one query...if on clicking radio button I want all the value related to that record,thn how can we achieve it..
for example:
when I click on 1st radio button I should have all (username,userid,address,zip,city,country)these values to be displyed on the screen....

by vczxv on Monday, April 20th 2009 at 06:19 AM

xvc b

by vczxv on Monday, April 20th 2009 at 06:19 AM

xvc b

by vczxv on Monday, April 20th 2009 at 06:19 AM

xvc b

by vinesh on Thursday, July 2nd 2009 at 07:19 AM

@vrushali
u shud look into jQuerry, its gives u neat methods to do that

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by bharathi on Monday, July 6th 2009 at 04:45 AM

hi
this s a wonderful tutorial for me.it helps me a lot.

by Zan on Monday, August 10th 2009 at 09:50 PM

Obviously you haven't gotten to writing an anti-bot form validation script. ;P

by adad on Wednesday, September 23rd 2009 at 05:55 AM

why doesn't water sink or drown ?

by waleed on Tuesday, December 8th 2009 at 06:07 AM

we neeed when we click radio button it show result in dialog box window

by Rakesh Gupta on Tuesday, January 5th 2010 at 03:41 AM

Hi..
This is Good tutorial for me.
But i want to chane the valu of the asp label control onclick of the radio button using javascript function.

Please Send me the Solution..

Thanx..

by Ricky on Wednesday, January 13th 2010 at 08:04 PM

I do'nt think it is true....

by quest on Sunday, April 25th 2010 at 01:13 PM

What if you want to check multiple group of radio buttons....
where name="" differs to each group.

by mathew on Wednesday, July 28th 2010 at 12:58 AM

Thanx alot,It is working yaar,I was using the getElementsByName method,thanx again

by mathew on Wednesday, July 28th 2010 at 12:59 AM

Thanx alot,It is working yaar,I was using the getElementsByName method,thanx again


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs JavaScript Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources