0

Hi i am creating this app and then i use javascript so that whatever i input in the boxes i could see it in my DETAILS HISTORY. and i did insert cells and stuff but when i run it i could see my table data getting inserted but only a few seconds then it will disappear i dont know why i hope you cn help me <3. here below is my codes

let enter = document.getElementById("btn");
enter.addEventListener("click", displayFunction);
let row = 1;
function displayFunction() {
  let detail = document.getElementById("detail-input").value;
  let amount = document.getElementById("amount-input").value;

  if (!detail || !amount) {
    alert("Please Fill in the Boxes");
    return;
  }

  let table = document.getElementById("myTable");
  let newRow = table.insertRow(row);

  let cell1 = newRow.insertCell(0);
  let cell2 = newRow.insertCell(1);

  cell1.innerHTML = detail;
  cell2.innerHTML = amount;
  row++;
}
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins";
}

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.685);
}

.form-container {
  min-width: 500px;
  min-height: 600px;
  background: linear-gradient(
    55deg,
    rgb(71, 126, 247),
    rgba(53, 52, 52, 0.295)
  );
  padding: 20px 30px;
  border: 5px solid rgb(37, 37, 49);
  border-radius: 10px;
  text-align: center;
  color: rgb(58, 57, 57);
  box-shadow: 2px 2px 20px #333;
  position: relative;
}

.form-container h1 {
  margin-bottom: 2rem;
}

.form-container h3 {
  margin-bottom: 2rem;
  position: relative;
}

.form-container h3::before {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  left: 20%;
}

.form-container h3::after {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  right: 20%;
}

form {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

form input {
  width: 100%;
  height: 100%;
  padding: 10px;
  background: none;
  outline: none;
  border: none;
  border-bottom: 2px solid rgb(41, 32, 32);
}

.detail {
  margin-top: 20px;
}

.amount {
  margin-top: 50px;
}

#btn {
  text-decoration: none;
  color: #000;
  border: 1px solid;
  border-radius: 20px;
  background: #fff;
  padding: 10px 20px;
  position: absolute;
  bottom: 28%;
  cursor: pointer;
  transition: 500ms ease-out;
}

#btn:hover {
  background: #000;
  color: #fff;
}

.history {
  margin-top: 80px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.col-2 {
  border: 1px solid;
  padding: 5px 10px;
  background: #fff;
}

.label-1 {
  color: green;
}

.label-2 {
  color: red;
}

.label-3 {
  color: rgb(24, 166, 223);
}

.details-container {
  margin-left: 10px;
  width: 500px;
  height: 600px;
  border: 5px solid #333;
  border-radius: 10px;
  text-align: center;
  background: linear-gradient(
    55deg,
    rgb(71, 126, 247),
    rgba(53, 52, 52, 0.295)
  );
}

.details-container h1 {
  margin-top: 20px;
}
#myTable {
  border-collapse: collapse;
  background: #fff;
  border: 1px solid;
  width: 100%;
}

#myTable,
td,
th {
  border: 2px solid;
  padding: 10px 20px;
}

td {
  color: #000;
}
<!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>Expenses Tracker App</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="form-container">
        <h1>EXPENSES TRACKER APP</h1>
        <h3>New Entry</h3>
        <form id="form">
          <div class="detail">
            <div class="col-1">
              <label>Details</label>
              <input type="text" id="detail-input" />
            </div>
          </div>
          <div class="amount">
            <div class="col-1">
              <label>Amount</label>
              <input type="number" id="amount-input" />
            </div>
          </div>
          <button id="btn">Enter</button>
        </form>

        <div class="history">
          <div class="col-2 col-ie">
            <p class="label-1">INFLOW</p>
            <p id="inflow">$0.00</p>
          </div>
          <div class="col-2">
            <p class="label-2">OUTFLOW</p>
            <p id="outflow">$0.00</p>
          </div>
          <div class="col-2">
            <p class="label-3">BALANCE</p>
            <p id="balance">$0.00</p>
          </div>
        </div>
      </div>

      <div class="details-container">
        <h1>DETAILS HISTORY</h1>
        <table id="myTable">
          <tr>
            <th>Details</th>
            <th>Amount</th>
          </tr>
        </table>
      </div>
    </div>
    <script src="app.js"></script>
  </body>
</html>

3
  • 1
    Change button to <button type="button" you are submitting the form Commented Apr 7, 2021 at 11:39
  • When you submit a form, the default behaviour is to reload the page. Silly behaviour if you ask me. You have to use event.preventDefault() to prevent that Commented Apr 7, 2021 at 11:41
  • ohmy god you know what after i posted this problem thats exactly what i thought and then i saw your comments its all true thank youuuuu so much !!!!!!! Commented Apr 7, 2021 at 11:43

2 Answers 2

1
  1. You are submitting the form - you can change the button to type="button" OR
  2. Don't use a submit button click to perform code, instead use the submit event and preventDefault()

let enter = document.getElementById("form").addEventListener("submit", displayFunction);

function displayFunction(e) {
  e.preventDefault(); <<<

let enter = document.getElementById("form").addEventListener("submit", displayFunction);
let row = 1;

function displayFunction(e) {
  e.preventDefault();
  let detail = document.getElementById("detail-input").value;
  let amount = document.getElementById("amount-input").value;

  if (!detail || !amount) {
    alert("Please Fill in the Boxes");
    return;
  }

  let table = document.getElementById("myTable");
  let newRow = table.insertRow(row);

  let cell1 = newRow.insertCell(0);
  let cell2 = newRow.insertCell(1);

  cell1.innerHTML = detail;
  cell2.innerHTML = amount;
  row++;
}
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins";
}

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.685);
}

.form-container {
  min-width: 500px;
  min-height: 600px;
  background: linear-gradient( 55deg, rgb(71, 126, 247), rgba(53, 52, 52, 0.295));
  padding: 20px 30px;
  border: 5px solid rgb(37, 37, 49);
  border-radius: 10px;
  text-align: center;
  color: rgb(58, 57, 57);
  box-shadow: 2px 2px 20px #333;
  position: relative;
}

.form-container h1 {
  margin-bottom: 2rem;
}

.form-container h3 {
  margin-bottom: 2rem;
  position: relative;
}

.form-container h3::before {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  left: 20%;
}

.form-container h3::after {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  right: 20%;
}

form {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

form input {
  width: 100%;
  height: 100%;
  padding: 10px;
  background: none;
  outline: none;
  border: none;
  border-bottom: 2px solid rgb(41, 32, 32);
}

.detail {
  margin-top: 20px;
}

.amount {
  margin-top: 50px;
}

#btn {
  text-decoration: none;
  color: #000;
  border: 1px solid;
  border-radius: 20px;
  background: #fff;
  padding: 10px 20px;
  position: absolute;
  bottom: 28%;
  cursor: pointer;
  transition: 500ms ease-out;
}

#btn:hover {
  background: #000;
  color: #fff;
}

.history {
  margin-top: 80px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.col-2 {
  border: 1px solid;
  padding: 5px 10px;
  background: #fff;
}

.label-1 {
  color: green;
}

.label-2 {
  color: red;
}

.label-3 {
  color: rgb(24, 166, 223);
}

.details-container {
  margin-left: 10px;
  width: 500px;
  height: 600px;
  border: 5px solid #333;
  border-radius: 10px;
  text-align: center;
  background: linear-gradient( 55deg, rgb(71, 126, 247), rgba(53, 52, 52, 0.295));
}

.details-container h1 {
  margin-top: 20px;
}

#myTable {
  border-collapse: collapse;
  background: #fff;
  border: 1px solid;
  width: 100%;
}

#myTable,
td,
th {
  border: 2px solid;
  padding: 10px 20px;
}

td {
  color: #000;
}
<div class="container">
  <div class="form-container">
    <h1>EXPENSES TRACKER APP</h1>
    <h3>New Entry</h3>
    <form id="form">
      <div class="detail">
        <div class="col-1">
          <label>Details</label>
          <input type="text" id="detail-input" />
        </div>
      </div>
      <div class="amount">
        <div class="col-1">
          <label>Amount</label>
          <input type="number" id="amount-input" />
        </div>
      </div>
      <button id="btn">Enter</button>
    </form>

    <div class="history">
      <div class="col-2 col-ie">
        <p class="label-1">INFLOW</p>
        <p id="inflow">$0.00</p>
      </div>
      <div class="col-2">
        <p class="label-2">OUTFLOW</p>
        <p id="outflow">$0.00</p>
      </div>
      <div class="col-2">
        <p class="label-3">BALANCE</p>
        <p id="balance">$0.00</p>
      </div>
    </div>
  </div>

  <div class="details-container">
    <h1>DETAILS HISTORY</h1>
    <table id="myTable">
      <tr>
        <th>Details</th>
        <th>Amount</th>
      </tr>
    </table>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

let enter = document.getElementById("btn");
enter.addEventListener("click", displayFunction);
let row = 1;
function displayFunction() {
  let detail = document.getElementById("detail-input").value;
  let amount = document.getElementById("amount-input").value;

  if (!detail || !amount) {
    alert("Please Fill in the Boxes");
    return;
  }

  let table = document.getElementById("myTable");
  console.log(table)
  let newRow = table.insertRow(row);

  let cell1 = newRow.insertCell(0);
  console.log(newRow);
  let cell2 = newRow.insertCell(1);

  cell1.innerHTML = detail;
  cell2.innerHTML = amount;
  row++;
  return;
}
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins";
}

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.685);
}

.form-container {
  min-width: 500px;
  min-height: 600px;
  background: linear-gradient(
    55deg,
    rgb(71, 126, 247),
    rgba(53, 52, 52, 0.295)
  );
  padding: 20px 30px;
  border: 5px solid rgb(37, 37, 49);
  border-radius: 10px;
  text-align: center;
  color: rgb(58, 57, 57);
  box-shadow: 2px 2px 20px #333;
  position: relative;
}

.form-container h1 {
  margin-bottom: 2rem;
}

.form-container h3 {
  margin-bottom: 2rem;
  position: relative;
}

.form-container h3::before {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  left: 20%;
}

.form-container h3::after {
  content: "";
  width: 4rem;
  height: 2px;
  background-color: rgb(41, 32, 32);
  position: absolute;
  top: 50%;
  right: 20%;
}

form {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

form input {
  width: 100%;
  height: 100%;
  padding: 10px;
  background: none;
  outline: none;
  border: none;
  border-bottom: 2px solid rgb(41, 32, 32);
}

.detail {
  margin-top: 20px;
}

.amount {
  margin-top: 50px;
}

#btn {
  text-decoration: none;
  color: #000;
  border: 1px solid;
  border-radius: 20px;
  background: #fff;
  padding: 10px 20px;
  position: absolute;
  bottom: 28%;
  cursor: pointer;
  transition: 500ms ease-out;
}

#btn:hover {
  background: #000;
  color: #fff;
}

.history {
  margin-top: 80px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.col-2 {
  border: 1px solid;
  padding: 5px 10px;
  background: #fff;
}

.label-1 {
  color: green;
}

.label-2 {
  color: red;
}

.label-3 {
  color: rgb(24, 166, 223);
}

.details-container {
  margin-left: 10px;
  width: 500px;
  height: 600px;
  border: 5px solid #333;
  border-radius: 10px;
  text-align: center;
  background: linear-gradient(
    55deg,
    rgb(71, 126, 247),
    rgba(53, 52, 52, 0.295)
  );
}

.details-container h1 {
  margin-top: 20px;
}
#myTable {
  border-collapse: collapse;
  background: #fff;
  border: 1px solid;
  width: 100%;
}

#myTable,
td,
th {
  border: 2px solid;
  padding: 10px 20px;
}

td {
  color: #000;
}
<!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>Expenses Tracker App</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="form-container">
        <h1>EXPENSES TRACKER APP</h1>
        <h3>New Entry</h3>
        <div id="form">
          <div class="detail">
            <div class="col-1">
              <label>Details</label>
              <input type="text" id="detail-input" />
            </div>
          </div>
          <div class="amount">
            <div class="col-1">
              <label>Amount</label>
              <input type="number" id="amount-input" />
            </div>
          </div>
          
        </div>
<button id="btn">Enter</button>
        <div class="history">
          <div class="col-2 col-ie">
            <p class="label-1">INFLOW</p>
            <p id="inflow">$0.00</p>
          </div>
          <div class="col-2">
            <p class="label-2">OUTFLOW</p>
            <p id="outflow">$0.00</p>
          </div>
          <div class="col-2">
            <p class="label-3">BALANCE</p>
            <p id="balance">$0.00</p>
          </div>
        </div>
      </div>

      <div class="details-container">
        <h1>DETAILS HISTORY</h1>
        <table id="myTable">
          <tr>
            <th>Details</th>
            <th>Amount</th>
          </tr>
        </table>
      </div>
    </div>
  </body>
</html>

thats correct its because you use form here is result after replacing with div. [https://jsfiddle.net/vishalpatil550/5xmu081o/7/][1]

1 Comment

agreed but if you are not submitting anything then we don't need a form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.