Handling HTML Forms - Part III


© Muhammad Ali Shah



Abstract

This is the third article of the series on HTML forms processing in JavaScript. This article assumes that you know how to write JavaScript functions and create HTML forms. See the last two articles if you are new to this topic. Today we will first analyze radio buttons and then some related topics. So, lets start the work!


Radio Buttons

Radio Buttons are used to select one option from a relatively short list. The only important property associated with a radio button is checked, which tells whether a radio button is selected or not. Following is an HTML code that displays three options for selection and displays a message when the button below them is clicked.

<html>
<head>
<title>HTML Form Processing</title>
<script language="JavaScript">
<!-- Hiding from old browsers
function check_form (form)
   {
   alert ("You cliced the button");
   }
// -->
</script >
</head>

<body>
<form name="survey">
How did you come to know about this site:<br>
<input type="radio" name="refer" value="search"> Search Engine<br>
<input type="radio" name="refer" value="magazine"> Some Magazine<br>
<input type="radio" name="refer"> value="surf"; Just surfed in!<br> <br>
<input type="button" name="submit" value="submit" onClick="check_form(survey)">
</form>

</body>
</html>

Code Explanation

Note a few things about the above code: First of all, the input type of the radio buttons is radio. Secondly, all of them have the same name. That makes sense because they are all part of the same question. These radio buttons differ only in their value attribute. Now try this: Copy, paste, save and then load this script into your browser and click the submit button without selecting any option. The alert message will still appear!

Now, how can you confirm a selection has been made before submitting this form. To comprehend the answer we need to know two things. The first one we will cover is arrays. We will briefly touch them. A more detailed discussion is deferred for some later article.

Array of Radio Buttons

An array, simply put, is a collection of objects referred to by their position in the collection. In the example given above, we have a collection of radio buttons named "refer", or in programming terms: an array "refer" of radio buttons. The value at the first position in an array is supposed to be 0. So, the search option is refer[0]. Similarly, the magazine option is refer[1] and the surf option is refer[1]. We can now re-write the check_form function to tell us the value of the option selected. The new function can also inform us if we click the button without selecting anything.

Go To Page: 1 2


The copyright of the article Handling HTML Forms - Part III in JavaScript is owned by . Permission to republish Handling HTML Forms - Part III in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

2.   Mar 22, 2001 2:41 AM
In response to message posted by ahunter:

In several C based languages (like C++, JavaScript and Java) the arrays start with index 0 ...


-- posted by ali_baba


1.   Feb 27, 2001 8:54 AM
When I select a radio button and then click the "Submit" button, the numbering for the buttons is 0-1-2. How can I change the numbering to start with 1? ...

-- posted by ahunter





For a complete listing of article comments, questions, and other discussions related to Muhammad Ali Shah's JavaScript topic, please visit the Discussions page.