Elaine Byene
Elaine Byene

Reputation: 4142

Get input value from a radio button

I'm trying to show the value in an input field when the radio-button is checked/clicked. But it does not work when I include bootstrap.min.js
JSFiddle

$('input:radio').click(function() {
      document.getElementById("whatever").innerHTML = $(this).val();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
      <div class="btn-group btn-group-toggle d-flex" data-toggle="buttons">
        <label class="btn btn-primary w-100 active">
                    <input type="radio" name="options" value="5" id="option1" autocomplete="off"> 5
                  </label>
        <label class="btn btn-primary w-100">
                    <input type="radio" name="options" value="10" id="option2" autocomplete="off"> 10
                  </label>
        <label class="btn btn-primary w-100">
                    <input type="radio" name="options" value="15" id="option3" autocomplete="off"> 15
                  </label>
        <label class="btn btn-primary w-100">
                    <input type="radio" name="options" value="20"id="option4" autocomplete="off"> 20
                  </label>
      </div>
    </div>
    <div class="container">
      <div class="form-group">
        <input id="whatever" class="form-control">
      </div>
    </div>

If the user clicks on say "15", then the value shows 15 in the input field.

PS: The code works when I disable the bootstrap.min.js and add <div id="whatever"></div>.

Upvotes: 0

Views: 181

Answers (6)

gaetanoM
gaetanoM

Reputation: 42054

PS: The code works when I disable the bootstrap.min.js and add <div id="whatever"></div>

For the second issue you need to change innerHTML with value.

For the first issue you need to change the event type.

Indeed, your main issue is in this line:

$('input:radio').click(function() {

For radio buttons you need to use the change event:

$(':radio').on('change', function() {

$(':radio').on('change', function() {
    $("#whatever").val($(this).val());
});
.container {
  margin-top: 30px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<div class="container">
    <div class="btn-group btn-group-toggle d-flex" data-toggle="buttons">
        <label class="btn btn-primary w-100 active">
            <input type="radio" name="options" value="5" id="option1" autocomplete="off"> 5
        </label>
        <label class="btn btn-primary w-100">
            <input type="radio" name="options" value="10" id="option2" autocomplete="off"> 10
        </label>
        <label class="btn btn-primary w-100">
            <input type="radio" name="options" value="15" id="option3" autocomplete="off"> 15
        </label>
        <label class="btn btn-primary w-100">
            <input type="radio" name="options" value="20"id="option4" autocomplete="off"> 20
        </label>
    </div>
</div>
<div class="container">
    <div class="form-group">
        <input id="whatever" class="form-control">
    </div>
</div>

Upvotes: 2

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21766

Use .value instead of .innerHTML with <input /> tags

.innerHTML:

div.innerHTML == "some text"

<div>some text</div>

.value:

input.value == "some text"

<input value="some text"/>

Hope it will be more clear now.

Below is working code:

$('input:radio').click(function() {
  document.getElementById("whatever").value = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="btn-group btn-group-toggle d-flex" data-toggle="buttons">
    <label class="btn btn-primary w-100 active">
                <input type="radio" name="options" value="5" id="option1" autocomplete="off"> 5
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="10" id="option2" autocomplete="off"> 10
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="15" id="option3" autocomplete="off"> 15
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="20"id="option4" autocomplete="off"> 20
              </label>
  </div>
</div>
<div class="container">
  <div class="form-group">
    <input id="whatever" class="form-control">
  </div>
</div>

Upvotes: 1

95faf8e76605e973
95faf8e76605e973

Reputation: 14201

In addition to the answers given: If you are having issues with bootstrap.min.js like you mentioned in your question, you can use change handler instead.

$(document).on('change', "input[type='radio']", function (event) {
  document.getElementById("whatever").value = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container">
  <div class="btn-group btn-group-toggle d-flex" data-toggle="buttons">
    <label class="btn btn-primary w-100 active">
                <input type="radio" name="options" value="5" id="option1" autocomplete="off"> 5
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="10" id="option2" autocomplete="off"> 10
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="15" id="option3" autocomplete="off"> 15
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="20"id="option4" autocomplete="off"> 20
              </label>
  </div>
</div>
<div class="container">
  <div class="form-group">
    <input id="whatever" class="form-control">
  </div>
</div>

Upvotes: 0

Sujan Gainju
Sujan Gainju

Reputation: 4767

You are using the innerHTML in the input field. Input field has only the val() or value property.

$('input:radio').click(function() {
  $("#whatever").val($(this).val());
});

OR

$('input:radio').click(function() {
  $("#whatever").value = $(this).val();
});

The JSFiddle snippet has been updated.

Upvotes: 1

4b0
4b0

Reputation: 22323

Don't mismatch try this way.

$('input:radio').click(function() {
  $("#whatever").val( $(this).val());
});
.container {
  margin-top: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="btn-group btn-group-toggle d-flex" data-toggle="buttons">
    <label class="btn btn-primary w-100 active">
                <input type="radio" name="options" value="5" id="option1" autocomplete="off"> 5
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="10" id="option2" autocomplete="off"> 10
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="15" id="option3" autocomplete="off"> 15
              </label>
    <label class="btn btn-primary w-100">
                <input type="radio" name="options" value="20"id="option4" autocomplete="off"> 20
              </label>
  </div>
</div>
<div class="container">
  <div class="form-group">
    <input id="whatever" class="form-control">
  </div>
</div>

Upvotes: 1

Igor
Igor

Reputation: 809

You mustn't use innerHTML for input, use value instead:

$('input:radio').click(function() {
  document.getElementById("whatever").value = $(this).val();
});

Upvotes: 3

Related Questions