skip to Main Content

I have 4 calculations that I have set up to spit out results when an integer 1-100 is typed into the input. right now, the values automatically update as I am typing, but I want them to wait until the calculate button is clicked. I can’t seem to figure out what I did wrong, or what I am missing.

let phoneOutput = document.getElementById("rc-phone");
let carOutput = document.getElementById("rc-car");
let trashOutput = document.getElementById("rc-trash");
let monthlyCost = document.getElementById("monthly-cost");
let input = document.getElementById("customPercent");
let ggrResults = document.querySelector("#customPercent");

ggrResults.addEventListener('input', e => adjustData(e.target.value)) // had to add in .value in order to extract the text from the input
const adjustData = (a) => {
//   convert value from input text to number in order to calculate
  const numberConvert = Number(a);
  const decimal = numberConvert/100;
  const monthlyCostFormula = (8000 * decimal) * (.026/12);
  const trashBagFormula = 243 * decimal;
  const milesDrivenFormula = 14294 * decimal;
  const phonesChargedFormula = 368949 * decimal;
  monthlyCost.innerHTML = monthlyCostFormula.toFixed(2);
  trashOutput.innerHTML = Math.round(trashBagFormula);
  carOutput.innerHTML = Math.round(milesDrivenFormula);
  phoneOutput.innerHTML = Math.round(phonesChargedFormula);
}
<form method="post">
  <input id="customPercent"  type="text" placeholder="custom" />
  <button type="submit" onclick="return(adjustData());" id="submit">Calculate</button>
</form>
<div>
  <h6>Equal to Removing</h6>
  <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-trash">100</span><br>
  <span class="green-text small-txt">Trash Bags of Waste from the Environment</span>
</div>
<div>    
  <h6>Equal to offsetting</h6>
  <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-car">20</span><br>
  <span class="green-text small-txt">Gas-Powered Miles Driven</span>
</div>
<div>
  <h6>Equal to offsetting</h6>
  <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-phone">500</span><br>
  <span class="green-text small-txt">Smartphones Charged</span>
</div>
<div>
  <h4 class="heading-4">Estimated Monthly Cost <a href="#" data-toggle="tooltip" title="This will have an explanation"><i class="fa fa-info-circle gray"></i></a></h4>
  <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;">$<span id="monthly-cost">15.00</span></span>
  <h5>per month</h5>   
</div>

3

Answers


  1. Did you mean something like this? Without the event listener and firing calculation only on click?

    const adjustData = () => {
    const input = document.getElementById("customPercent");
    const phoneOutput = document.getElementById("rc-phone");
    const carOutput = document.getElementById("rc-car");
    const trashOutput = document.getElementById("rc-trash");
    const monthlyCost = document.getElementById("monthly-cost");
    const ggrResults = document.querySelector("#customPercent");
    
      // convert value from input text to number in order to calculate
      const numberConvert = Number(input.value);
      const decimal = numberConvert/100;
      const monthlyCostFormula = (8000 * decimal) * (.026/12);
      const trashBagFormula = 243 * decimal;
      const milesDrivenFormula = 14294 * decimal;
      const phonesChargedFormula = 368949 * decimal;
      monthlyCost.innerHTML = monthlyCostFormula.toFixed(2);
      trashOutput.innerHTML = Math.round(trashBagFormula);
      carOutput.innerHTML = Math.round(milesDrivenFormula);
      phoneOutput.innerHTML = Math.round(phonesChargedFormula);
    }
    <form method="post">
      <input id="customPercent"  type="text" placeholder="custom" />
      <button type="button" onclick="adjustData();" id="submit">Calculate</button>
    </form>
    <div>
      <h6>Equal to Removing</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-trash">100</span><br>
      <span class="green-text small-txt">Trash Bags of Waste from the Environment</span>
    </div>
    <div>    
      <h6>Equal to offsetting</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-car">20</span><br>
      <span class="green-text small-txt">Gas-Powered Miles Driven</span>
    </div>
    <div>
      <h6>Equal to offsetting</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-phone">500</span><br>
      <span class="green-text small-txt">Smartphones Charged</span>
    </div>
    <div>
      <h4 class="heading-4">Estimated Monthly Cost <a href="#" data-toggle="tooltip" title="This will have an explanation"><i class="fa fa-info-circle gray"></i></a></h4>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;">$<span id="monthly-cost">15.00</span></span>
      <h5>per month</h5>   
    </div>
    Login or Signup to reply.
  2. If I understand it right:

    <form method="post" action="#" onsubmit="return false">
    To prevent page refresh/redirection: "#" is a empty anchor and "return false" is a js soltution.

    ggrResults.addEventListener('input', e => adjustData(e.target.value))
    Quoted out.

    <button type="submit" onclick="adjustData(document.getElementById('customPercent').value)";" id="submit">Calculate</button>
    onclick calls the function and pass the value by get the input object’s value by the id "customPercent".

    let phoneOutput = document.getElementById("rc-phone");
    let carOutput = document.getElementById("rc-car");
    let trashOutput = document.getElementById("rc-trash");
    let monthlyCost = document.getElementById("monthly-cost");
    let input = document.getElementById("customPercent");
    let ggrResults = document.querySelector("#customPercent");
    
    // had to add in .value in order to extract the text from the input
    // ggrResults.addEventListener('input', e => adjustData(e.target.value))
    const adjustData = (a) => {
    //   convert value from input text to number in order to calculate
      const numberConvert = Number(a);
      const decimal = numberConvert/100;
      const monthlyCostFormula = (8000 * decimal) * (.026/12);
      const trashBagFormula = 243 * decimal;
      const milesDrivenFormula = 14294 * decimal;
      const phonesChargedFormula = 368949 * decimal;
      monthlyCost.innerHTML = monthlyCostFormula.toFixed(2);
      trashOutput.innerHTML = Math.round(trashBagFormula);
      carOutput.innerHTML = Math.round(milesDrivenFormula);
      phoneOutput.innerHTML = Math.round(phonesChargedFormula);
    }
    <form method="post" action="#" onsubmit="return false">
      <input id="customPercent"  type="text" placeholder="custom" />
      <button type="submit" onclick="adjustData(document.getElementById('customPercent').value)";" id="submit">Calculate</button>
    </form>
    <div>
      <h6>Equal to Removing</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-trash">100</span><br>
      <span class="green-text small-txt">Trash Bags of Waste from the Environment</span>
    </div>
    <div>    
      <h6>Equal to offsetting</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-car">20</span><br>
      <span class="green-text small-txt">Gas-Powered Miles Driven</span>
    </div>
    <div>
      <h6>Equal to offsetting</h6>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;" id="rc-phone">500</span><br>
      <span class="green-text small-txt">Smartphones Charged</span>
    </div>
    <div>
      <h4 class="heading-4">Estimated Monthly Cost <a href="#" data-toggle="tooltip" title="This will have an explanation"><i class="fa fa-info-circle gray"></i></a></h4>
      <span style="font-size: 28px; font-weight: bold; font-family: sans-serif;">$<span id="monthly-cost">15.00</span></span>
      <h5>per month</h5>   
    </div>
    Login or Signup to reply.
  3. something like that ?

    const
      myForm      = document.querySelector('#my_form')  
    , phoneOutput = document.querySelector('#rc-phone')
    , carOutput   = document.querySelector('#rc-car')
    , trashOutput = document.querySelector('#rc-trash')
    , monthlyCost = document.querySelector('#monthly-cost')
      ;
    myForm.addEventListener('submit', e =>
      { 
      e.preventDefault();  // disable form submission (and page reload)
    
      const
        decimal              = myForm.customPercent.valueAsNumber / 100 // get value
      , monthlyCostFormula   = (8000 * decimal) * (.026/12)
      , trashBagFormula      =   243 * decimal
      , milesDrivenFormula   = 14294 * decimal 
      , phonesChargedFormula = 368949 * decimal
        ;
      monthlyCost.textContent = monthlyCostFormula.toFixed(2);
      trashOutput.textContent = Math.round(trashBagFormula);
      carOutput.textContent   = Math.round(milesDrivenFormula);
      phoneOutput.textContent = Math.round(phonesChargedFormula);
      }
    )
    div > span:first-of-type {
      font-size   : 28px; 
      font-weight : bold; 
      font-family : sans-serif;
      }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    
    <form id="my_form">
      <input name="customPercent"  type="number" placeholder="custom" min="0" max="100" required >
      <button type="submit">Calculate</button>
    </form>
    <div>
      <h6>Equal to Removing</h6>
      <span id="rc-trash">100</span><br>
      <span class="green-text small-txt">Trash Bags of Waste from the Environment</span>
    </div>
    <div>    
      <h6>Equal to offsetting</h6>
      <span id="rc-car">20</span><br>
      <span class="green-text small-txt">Gas-Powered Miles Driven</span>
    </div>
    <div>
      <h6>Equal to offsetting</h6>
      <span id="rc-phone">500</span><br>
      <span class="green-text small-txt">Smartphones Charged</span>
    </div>
    <div>
      <h4 class="heading-4">
        Estimated Monthly Cost 
        <a href="#" data-toggle="tooltip" title="This will have an explanation">
          <i class="fa fa-info-circle gray"></i>
        </a>
      </h4>
      <span>$<span id="monthly-cost">15.00</span></span>
      <h5>per month</h5>   
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search