This script writes the value of a radio button and a text input to a textarea.
Because we want the user to be able to change their radio button selection, we first have to check to see if a selection from a radio group has already been made. If a selection has been made, the string from the radio group is replaced with the value of the clicked button.
This is accomplished by the
replace() method, along with a little help from the indexOf() method which is used to search for an instance of a specified string.
The replace method takes two parameters: the string to search for and the string that will replace it if it is found:
string.replace("find this","replace with this")
In this example, we're replacing the first parameter with the value of the argument, "text":
string.replace("find this",text)
Since that value is set when the function is called, text can be used as a generic value for the 2nd parameter and is defined within the radio buttons.
<input type="radio" name="why"
onClick="whyNot('My homework wasn't done because I'm lazy.')">
View the Source