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

249
Views
Display Correct Radio Button Value Not Working with Default Checked Option?

Problem: Default checked radio button value shows on app, but if I change the tip value, then unselect and reselect a checkbox, it goes back to the original checked radio button value instead of changing to the new tip value.

Basicaly, my default radio checked button value is set to $5. If I select a checkbox option, then I change the tip value to $2 then unselect a checkbox, then reselect a checkbox, it will show the tip value as $5 instead of the new option $2

Here's my codepen. I added instructions on the codepen so you can see my problem

https://codepen.io/jaysomo10/pen/dydaKwZ

Here's my JS

window.menuItems = 0;
window.tips = 0;

const foodTotal = document.getElementById("price");
const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

let tipVal = document.querySelector(".tips:checked").value;

tipTotal.textContent = "$" + (tipVal / 100).toFixed(2);

document.addEventListener("click", ({ target }) => {
  if (target.className === "food" && target.checked && tipVal) {
    window.menuItems += parseInt(target.value);
    window.tips = tipVal;
  } else if (target.className === "food" && !target.checked) {
    window.menuItems -= parseInt(target.value);
  } else if (target.className === "tips" && target.checked) {
    window.tips = parseInt(target.value);
  } else {
    return;
  }

  foodTotal.textContent = `$${(window.menuItems / 100).toFixed(2)}`;
  tipTotal.textContent = `$${(window.tips / 100).toFixed(2)}`;
  orderTotal.textContent = `$${(
    (Number(window.menuItems) + Number(window.tips)) /
    100
  ).toFixed(2)}`;
});

I can't seem to convert the logic to make the tip value to change after I unselect & re select a checkbox option.

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

0

tipVal is defined outside the click event. So it has always the value at the sart. You have to changhe it also inside the event

window.menuItems = 0;
window.tips = 0;

const foodTotal = document.getElementById("price");
const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");

let tipVal = document.querySelector(".tips:checked").value;
tipTotal.textContent = "$" + (tipVal / 100).toFixed(2);

document.addEventListener("click", ({ target }) => {
  tipVal = document.querySelector(".tips:checked").value;
  if (target.className === "food" && target.checked && tipVal) {
    window.menuItems += parseInt(target.value);
    window.tips = tipVal;
  } else if (target.className === "food" && !target.checked) {
    window.menuItems -= parseInt(target.value);
  } else if (target.className === "tips" && target.checked) {
    window.tips = parseInt(target.value);
  } else {
    return;
  }

  foodTotal.textContent = `$${(window.menuItems / 100).toFixed(2)}`;
  tipTotal.textContent = `$${(window.tips / 100).toFixed(2)}`;
  orderTotal.textContent = `$${(
    (Number(window.menuItems) + Number(window.tips)) /
    100
  ).toFixed(2)}`;
});
about 4 years ago · Juan Pablo Isaza Report

0

remove this from your first if statement

if (target.className === "food" && target.checked && tipVal) {
    window.menuItems += parseInt(target.value);
    //window.tips = tipVal; // remove this
  }
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!