1

I used Bootstrap to create the radio buttons:

<div id="heightForm">
  <legend class="h">
    Height:
    <div class="form-check form-check-inline weightChoose commonRadio">
      <div class="form-check form-check-inline kg">
        <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="METER" id="h1"/>
        <label class="form-check-label lbl" for="h1">Meter</label>
      </div>
      <div class="form-check form-check-inline pound">
        <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="FEET" id="h2"/>
        <label class="form-check-label lbl" for="h2">Feet</label>
      </div>
    </div>
  </legend>
</div>

i wanted to console.log(heightIn);

where

var heightIn = $('input[name="heightButtonS"]:checked').val();

but it gives undefined.

i also tried:

var heightIn = $('input[name=heightButtonS]:checked').val();
var heightIn = $('input[name="heightButtonS"]:checked').value();

This does return the value when there is an attribute "checked" in one of the buttons. Even on selecting the other button it returns the value of that assigned button. I want it to retrieve value for the button which is selected.

Does this not work anymore?

10
  • 1
    When are you assigning the variable? You need to wait for the user to select one of the buttons. Please post a minimal reproducible example. Commented May 15 at 16:46
  • So the JS should be in an event listener. Commented May 15 at 16:47
  • Are you including jquery? You didn't tag it in your question. Commented May 15 at 17:00
  • It will be undefined when $('input[name="heightButtonS"]:checked').length === 0 - ie before the user has selected one of the radios (as noted above, just adding the .length confirmation) Commented May 15 at 17:00
  • 1
    @MisterJojo, a form is only necessary if submitting it traditionally. If you're using ajax or not submitting, a form is not necessary. Commented May 15 at 17:11

3 Answers 3

1

A simple way to code this

const myForm = document.querySelector('#my-form');

myForm.oninput = e =>
  {
  console.clear();
  console.log( myForm.heightButtonS.value );
  }
label { display: block; }
<form id="my-form">
  <fieldset>
    <legend> Height: </legend>
    <label>
      <input type="radio" name="heightButtonS" value="METER">
      Meter
    </label>
    <label>
      <input type="radio" name="heightButtonS" value="FEET" >
      Feet
    </label>
  </fieldset>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to make sure you included jQuery and Bootstrap as well as create a change event for the elements whose value you want to track and get the value inside the event handler.

$('input[name="heightButtonS"]').change(function() {
    console.log(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4Q6Gf2aSP4eDXB8Miphtr37CMZZQ5oXLH2yaXMJ2w8e2ZtHTl7GptT4jmndRuHDT" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-j1CDi7MgGQ12Z7Qab0qlWQ/Qqz24Gc6BM0thvEMVjHnfYGF0rmFCozFSxQBxwHKO" crossorigin="anonymous"></script>
<div id="heightForm">
  <legend class="h">
    Height:
    <div class="form-check form-check-inline weightChoose commonRadio">
      <div class="form-check form-check-inline kg">
        <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="METER" id="h1"/>
        <label class="form-check-label lbl" for="h1">Meter</label>
      </div>
      <div class="form-check form-check-inline pound">
        <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="FEET" id="h2"/>
        <label class="form-check-label lbl" for="h2">Feet</label>
      </div>
    </div>
  </legend>
</div>

Comments

0

You get the value of a radiolist by it's value property. Don't use a queryselector (jQuery etc.) for this.

In the case of the radio buttons, the queryselector will just get one of the elements, whereas form.heightButtonS is the collection aka. a radioList, and the radioList has the selected value. Actually (in the following example), you can also get the value with e.target.value now that the input element that was selecte is tied to the change event.

document.forms.form01.addEventListener('change', e => {
  let form = e.target.form;
  console.log(form.heightButtonS.value, e.target.value);
});
<form name="form01">
  <div id="heightForm">
    <legend class="h">
      Height:
      <div class="form-check form-check-inline weightChoose commonRadio">
        <div class="form-check form-check-inline kg">
          <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="METER" id="h1"/>
          <label class="form-check-label lbl" for="h1">Meter</label>
        </div>
        <div class="form-check form-check-inline pound">
          <input class="form-check-input" type="radio" name="heightButtonS" role="radio" value="FEET" id="h2"/>
          <label class="form-check-label lbl" for="h2">Feet</label>
        </div>
      </div>
    </legend>
  </div>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.