How can I add an "Other" text input to a set of radio buttons in an HTML form?
Asked Answered
P

6

6

I'm trying to create a form with a write-in "other" option as the last radio button. I've tried variants of the following with no success:

<label><input type="radio" name="candidate" value="option1">First Option</label>
<label><input type="radio" name="candidate" value="option2">Second Option</label>
<label><input type="radio" name="candidate" value="other">OTHER</label><input type="text" name="other" value="Write In" />

Is it possible to have a radio button with a text input that passes its value to that radio button on submit without using javascript?

Plover answered 4/8, 2009 at 16:21 Comment(0)
B
3

If 'Other' is selected, you would want to submit only the value in the text field, it does not make sense to feed that into the radio and pass that to the server. You could give the value of the 'Other' radio the empty string, e.g.:

<label><input type="radio" name="candidate" value="">OTHER</label>

And on the server if no value is present in "candidate" look to "other".

Beadsman answered 4/8, 2009 at 16:28 Comment(0)
R
5

This is the simplest way I can see, please correct me if I am wrong!

<form name="fruitForm" method="post" action="other-post.php">
<label><input type="radio" name="fruit" value="apple" onClick="regularFruit()">apple</input></label>
<label><input type="radio" name="fruit" value="orange" onClick="regularFruit()">orange</input></label>
<label><input type="radio" name="fruit" value="lemon" onClick="regularFruit()">lemon</input></label>
<label onClick="otherFruit()">
    <input type="radio" name="fruit" id="other_fruit" value="other" >or other fruit:</input>
    <input type ="text" name="other" id="other_text"/></label>
    <input type="submit" value="Submit">
</form>

<script>
function otherFruit(){
a=document.getElementById('other_fruit');
a.checked=true;
}
function regularFruit(){
a=document.getElementById('other_text');
a.value="";
}
</script>

Where the label incorporates the radio and the text field, the text field checks the "other" radio, and sends the value of "other" as what is inputted in the field. Then checking a radio button again clears the text field

Roughhew answered 8/3, 2014 at 16:26 Comment(0)
B
3

If 'Other' is selected, you would want to submit only the value in the text field, it does not make sense to feed that into the radio and pass that to the server. You could give the value of the 'Other' radio the empty string, e.g.:

<label><input type="radio" name="candidate" value="">OTHER</label>

And on the server if no value is present in "candidate" look to "other".

Beadsman answered 4/8, 2009 at 16:28 Comment(0)
B
2

No - this isn't supported by the HTML specifications. You'd either need to do it via javascript before the form is submitted, or better still, check if the radio button is set to "other" and then read the textbox value AFTER the form is submitted.

One reason you don't want to really mess around with altering the return value of the "other" radio button is what if I was to type "option1" in your text box?

Brute answered 4/8, 2009 at 16:27 Comment(0)
S
0

I don't believe the HTML spec supports this, no. You'd just have to have your processing code look at the radio button, see that the value is 'other', and go get a value from the text field's input (the 'other' var in the form submission instead of the 'candidate' var).

Swinge answered 4/8, 2009 at 16:26 Comment(0)
S
0

No. This requires dynamic updates to the DOM, which requires javascript.

Skillful answered 4/8, 2009 at 16:27 Comment(0)
B
0

Here's what I did (run the snippet to see what it does). Hopefully you can poke around and figure out how it works.

When processing the form, you can check the values by the fact that either a radio button is checked or the textbox will have a value.

$(document).ready(
  function() {
    $('input[name=amount]').on('click', function() {
      var customText = $('input[name=custom-amount]');
      customText.val('');
      customText.parent().removeClass("selected");
    });

    $('input[name=custom-amount]').on('click', function() {
      $('input[name=amount]').attr('checked', false);
      $(this).parent().addClass("selected");
    });
  });
.donate {
  font-family: sans-serif;
}
.donate .payee {
  color: #4B8EBC;
  font-weight: bold;
}
.donate .custom-amount {
  width: 150px;
  height: 40px;
  background-color: #7DB6DA;
  color: #325F7F;
  font-weight: bold;
  font-size: 1rem;
  padding: 2px;
  padding-left: 6px;
}
.donate .custom-amount input {
  font-weight: lighter;
  font-size: 0.8rem;
  background-color: white;
  width: 116px;
  height: 24px;
  margin-top: 4px;
  margin-left: 6px;
}
.donate .radio-control {
  position: relative;
  display: inline-block;
  font-size: 1rem;
  width: 50px;
  height: 40px;
  cursor: pointer;
  z-index: 12;
}
.donate .radio-control input[type="radio"] {
  position: absolute;
  z-index: -1;
  opacity: 0;
}
.donate .radio-control__indicator {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  text-align: center;
  line-height: 40px;
}
.donate .radio-control__indicator, .donate .custom-amount {
  background-color: #7DB6DA;
  color: #325F7F;
}
.donate .radio-control:hover input ~ .radio-control__indicator,
.donate .radio-control input:focus ~ .radio-control__indicator {
  opacity: 0.9;
}
.donate .radio-control input:checked ~ .radio-control__indicator,
.donate .custom-amount.selected {
  background: #4B8EBC;
  color: white;
}
.donate .radio-control:hover input:not([disabled]):checked ~ .radio-control__indicator,
.donate .radio-control input:checked:focus ~ .radio-control__indicator {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="donate">
  <div class="row amount">
    <div class="control-group">
      <label class="radio-control">
        <input checked="checked" name="amount" type="radio" value="$5" />
        <div class="radio-control__indicator">
          $5
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$10" />
        <div class="radio-control__indicator">
          $10
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$20" />
        <div class="radio-control__indicator">
          $20
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$50" />
        <div class="radio-control__indicator">
          $50
        </div>
      </label>
      <div class="custom-amount">
        $
        <input class="custom-amount" name="custom-amount" placeholder="Custom amount" size="40" />
      </div>
    </div>
  </div>
</form>
Boote answered 10/5, 2016 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.