0

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>.

95faf8e76605e973
  • 13,643
  • 3
  • 24
  • 51
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96
  • `#whatever` is an `input` box, which doesn't have `innerHTML` property. You should use `value` instead. – 31piy Aug 23 '18 at 11:49
  • Possible duplicate of [Set the value of an input field](https://stackoverflow.com/questions/7609130/set-the-value-of-an-input-field) – 31piy Aug 23 '18 at 11:50

6 Answers6

3

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

$('input:radio').click(function() {
  document.getElementById("whatever").value = $(this).val();
});
Igor
  • 809
  • 5
  • 16
2

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>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
1

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>
4b0
  • 21,981
  • 30
  • 95
  • 142
1

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>
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
1

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.

Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
0

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>
95faf8e76605e973
  • 13,643
  • 3
  • 24
  • 51