0

Designer who is trying to learn javascript, so I'm making a basic app that calculates tax and a 20% tip for a bill. Right now, I'm just trying to get the total with tax to display. The answer is there when I console.log() it, and it appears for a second and then disappears.

const subTotal = document.getElementById('subtotal');
const submit = document.getElementById('form-submit');
const taxes = document.getElementById('taxes');
const totalAmount = document.getElementById('total-amount');

submit.addEventListener('click', calculate);

function calculate(e) {
  let subby = subTotal.value;
  let taxTotal = subby * (1 + 0.07);
  console.log(taxTotal);
  //doesn't work at all
  //taxes.textContent = taxTotal;

  //works temporarily
  taxes.value = taxTotal;

}
body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: helvetica, Arial, sans-serif;
}

.container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  margin: 0;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(to right, #119ae9, #46d2da);
}

.form {
  background: rgb(240, 240, 240);
  width: 80%;
  height: 80vh;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  /*justify-content: center;*/
  align-items: center;
}

.form-style {
  width: 80%;
  outline: none;
  border: none;
  font-size: 20px;
  padding: 8px;
}

.form-style:active {
  /*outline: 2px solid blue;*/
}

p {
  font-size: 20px;
}

#form-submit {
  padding: 4px 6px;
  margin-top: 30px;
  margin-bottom: 30px;
  background: #119ae9;
  border: none;
  outline: none;
  cursor: pointer;
  color: #fff;
  width: 180px;
  font-size: 20px;
}

#form-submit:hover {
  background: #46d2da;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="container">
    <h1>Bill Calculator</h1>
    <form class="form">
      <p>Please Enter Your Bill</p>
      <input type="number" id="subtotal" class="form-style">
      <!--button-->
      <input type="submit" id="form-submit" value="CALCULATE">
      <!--taxes-->
      <p>Total with Taxes</p>
      <input type="number" id="taxes" class="form-style">
      <!--tip-->
      <p>20% Tip</p>
      <input type="number" id="tip" class="form-style" disabled>

      <!--total-->
      <p>Total Amount</p>
      <input type="number" id="total-amount" class="form-style" disabled>
    </form>
  </div>
</body>

</html>

I do get confused between innerHTML and textContent, and I couldn't get the desired result with either.

Appreciate any help.

Barmar
  • 741,623
  • 53
  • 500
  • 612
baboles
  • 25
  • 4

0 Answers0