Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to change text in two paragraphs if one of the radio buttons is checked? JS

I have a form where based on which radio button is checked (Metric or Imperial), the two paragraphs should display either KG + CM or LBS + IN. Right now it just displays KG or LBS twice. Looking for a solution in vanilla JS. This is for a calculator I am working on.

const paragraphWeightElement = document.querySelector(".js-paragraphWeight");
const paragraphHeightElement = document.querySelector(".js-paragraphHeight");
const form = document.querySelector(".js-form");

form.addEventListener("input", (event) => {
  paragraphWeightElement.innerText = event.target.value;
  paragraphHeightElement.innerText = event.target.value;
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script defer src="js/script.js"></script>
    <link href="css/style.css" rel="stylesheet" />
  </head>
  <body>
    <form class="js-form">
      <ul>
        <li>
          <label>
            <input class="js-metric" type="radio" value="KG" name="weight" />
            Metric
          </label>
        </li>
        <li>
          <label class="form__label">
            <input class="js-imperial" type="radio" value="LBS" name="weight" />
            Imperial
          </label>
        </li>
      </ul>
    </form>
    <p class="js-paragraphWeight"></p>
    <p class="js-paragraphHeight"></p>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to not work with event.target but rather find the element checked.

For the unit lookup, use a simple lookup map.

const paragraphWeightElement = document.querySelector(".js-paragraphWeight");
const paragraphHeightElement = document.querySelector(".js-paragraphHeight");
const form = document.querySelector(".js-form");

const unitLookupMap = {
  metric:   { weight: 'kg',  height: 'meters' },
  imperial: { weight: 'lbs', height: 'yards' },
}

form.addEventListener("input", (event) => {
  const checked = form.querySelector('[name=units]:checked');
  paragraphWeightElement.textContent = unitLookupMap[checked.value].weight;
  paragraphHeightElement.textContent = unitLookupMap[checked.value].height;
});
<form class="js-form">
  <ul>
    <li>
      <label>
            <input class="js-metric" type="radio" value="metric" name="units" />
            Metric
          </label>
    </li>
    <li>
      <label class="form__label">
            <input class="js-imperial" type="radio" value="imperial" name="units" />
            Imperial
          </label>
    </li>
  </ul>
</form>
<p class="js-paragraphWeight"></p>
<p class="js-paragraphHeight"></p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!