Code ExplanationNote 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 ButtonsAn 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.