Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Forms: Radio buttons with text fields

Tags:

html

forms

This seems like a simple problem, but how do I create a basic HTML form that has a series of radio button options, with the last one being a text field to fill in a custom response (i.e. "Other").

What I have now:

    Reason given for stop? <br>
    <input type="radio" name="reason" value="Fit Description">Fit Description<br>
    <input type="radio" name="reason" value="Suspicious Behavior">Suspicious Behavior<br>
    <input type="radio" name="reason" value="No Reason Given">No Reason Given<br>
    <input type="radio" name="reason" value="">Other<br>
like image 283
alyx Avatar asked Oct 15 '12 02:10

alyx


People also ask

How can I add an other text input to a set of radio buttons in an HTML form?

Just add a text input field to it. Keep in mind, the name "other_reason" doesn't actually connect to the radio button with name of "reason." So, when submitting the form, the value of "Other" doesn't actually pass through unless you specifically ask for "other_reason."

How do you code radio buttons in HTML?

The <input type="radio"> defines a radio button. Radio buttons are normally presented in radio groups (a collection of radio buttons describing a set of related options). Only one radio button in a group can be selected at the same time.

How can you create Radiobutton and checkbox in HTML write HTML code for it?

Checkbox allows one or many options to be selected. It is created by using HTML <input> tag but type attribute is set to radio. It is also created using HTML <input> tag but type attribute is set to checkbox.


2 Answers

Just add a text input field to it.

Reason given for stop? <br>
    <input type="radio" name="reason" value="Fit Description">Fit Description<br>
    <input type="radio" name="reason" value="Suspicious Behavior">Suspicious Behavior<br>
    <input type="radio" name="reason" value="No Reason Given">No Reason Given<br>
<input type="radio" name="reason" value="">Other <input type="text" name="other_reason" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

jsFiddle example

like image 82
j08691 Avatar answered Oct 12 '22 18:10

j08691


  1. Create a text field, and set it to display:none;
  2. Then with jQuery, detect when the 'Other' radio button is checked and show the textbox.
  3. Then on your process script, do and if statement to see if the value of your radio button group is "" (nothing), and grab the post data of the textbox and do what you want with it.
like image 27
Anthony Avatar answered Oct 12 '22 19:10

Anthony