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

193
Views
How to clear an input of type number when a button is clicked

I am making a mortgage calculator and am trying to clear all the inputs when the clear button is presses. I can't seem to get it to work. Below is my html and JavaScript code, I have also tried setting the inputs = null and that didn't work.

HTML:

<div class="calculator">
    <h1>Mortgage Calculator</h1>
    <div class="input-container">
        <label for="Loan-amount">Total Loan Amount</label>
        <input type="number" name="Loan-amount" id="total" min="0">
    </div>

    <div class="input-container">
        <label for="down-payment">Down payment</label>
        <input type="number" name="Loan-amount" id="down" min="0">
    </div>

    <div class="input-container">
        <label for="interest-rate">Interest rate %</label>
        <input type="number" name="interest-rate" id="interest" min="0">
    </div>

    <div class="input-container">
        <label for="loan-term">Loan Term (in years)</label>
        <input type="number" name="loan-term" id="duration" min="0">
    </div>
    <div class="answer">
        <h2>Estimated payment:</h2>
        <p id="paragraph-value"></p>
    </div>
    <div class="button-container">
        <button id="submitBtn">Calculate</button>
        <button id="clearBtn">Clear</button>
    </div>
    <p id="alert"></p>
</div>

JavaScript:

const clearBtn = document.querySelector("#clearBtn");

clearBtn.addEventListener("click", function (e) {
  let total = document.getElementById("total").value;
  let interest = document.querySelector("#interest").value;
  let duration = document.querySelector("#duration").value;
  let downPayment = document.querySelector("#down").value;

  total = "";
  interest = "";
  duration = "";
  downPayment = "";
});
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You're not setting a value to the inputs, you're just over-writing the value of a varable:

total = "";

To set the value of the input, you'd set the .value property on the input:

document.getElementById("total").value = "";

For example:

const clearBtn = document.querySelector("#clearBtn");

clearBtn.addEventListener("click", function (e) {
  document.getElementById("total").value = "";
});
<input type="number" id="total" />
<button id="clearBtn">Reset</button>


At a more generic level, you seem to be confused about the difference between these two things:

var total = document.getElementById("total").value;
total = "";

and:

var total = document.getElementById("total");
total.value = "";

In the first case the variable holds a copy of the value itself, and you're re-assigning the variable to a new value. This does nothing to the element.

But in the second case the variable holds a reference to the element, and you're updating a property on that element.

about 4 years ago · Santiago Gelvez 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!