Yesha Majmundar
Yesha Majmundar

Reputation: 49

Insert form values in a table using javascript

document.getElementById("submit").onclick = function () {
  document.getElementById("table").style.display = "block";

  var table = document.getElementById("table");
  var row = table.insertRow(-1);
  var date = row.insertCell(0);
  var desc = row.insertCell(1);
  var amt = row.insertCell(2);
  date.innerHTML = document.getElementById("date").value;
  desc.innerHTML = document.getElementById("desc").value;
  amt.innerHTML = document.getElementById("amount").value;

  return false;
}
table {
  display: none;
}
<h1>Expense Tracker</h1>

<form action="">
  <label id="date">Date: </label>
  <input type="date" id="date">
  <br>

  <label id="desc">Description: </label>
  <input type="text" id="desc">
  <br>

  <label id="amount">Amount: </label>
  <input type="text" id="amount">
  <br>

  <input type="button" id="submit" value="Submit">
  <br>
</form>

<table id="table">
  <tr>
    <th>Date</th>
    <th>Description</th>
    <th>Amount</th>
  </tr>
</table>

I want to insert the input given in the form into the table by clicking the submit button. I tried doing it using this method but it always displays undefined. Where am I going wrong? Is there a better alternative to do this? I want to use Javascript only. This is the output that I'm getting

Upvotes: 1

Views: 16352

Answers (2)

Kylro
Kylro

Reputation: 161

The problem are your labels. They have the same ids as your input fields. Since document.getElementById("date") only finds the first occurrence of the desired id your labels are returned.

To solve this you can change your labels to

<label for="date">Date: </label>

<html>
    <head>
        <title>Expense Tracker</title>
        
        <style type="text/css">
            table
            {
                display: none;
            }
        </style>
    </head>
    
    <body>
        <h1>Expense Tracker</h1>
        
        <form action="">
            <label for="date">Date: </label>
            <input type="date" id="date">
            <br>
            
            <label for="desc">Description: </label>
            <input type="text" id="desc">
            <br>
            
            <label for="amount">Amount: </label>
            <input type="text" id="amount">
            <br>
            
            <input type="button" id="submit" value="Submit">
            <br>
        </form>
        
        <table id="table">
            <tr>
                <th>Date</th>
                <th>Description</th>
                <th>Amount</th>
            </tr>
        </table>
    
        <script type="text/javascript">
            
            document.getElementById("submit").onclick=function()
            {
                document.getElementById("table").style.display="block";
                
                var table = document.getElementById("table");
                var row = table.insertRow(-1);
                var date = row.insertCell(0);
                var desc = row.insertCell(1);
                var amt = row.insertCell(2);
                date.innerHTML = document.getElementById("date").value;
                desc.innerHTML = document.getElementById("desc").value;
                amt.innerHTML = document.getElementById("amount").value;
                
                return false;
            }
        
        </script>
    </body>
</html>

Upvotes: 6

Derek Wang
Derek Wang

Reputation: 10193

On your html file, each <label> and <input> tags have got the same id so the problem happened.

For example, for the last input, the label has id amount and the input tag also has id amount.

So document.getElementById("amount") will return the first tag <label> tag so it won't have no values.

To solve this problem, it is needed to change the id of the labels so no duplicates.

document.getElementById("submit").onclick = function () {
  document.getElementById("table").style.display = "block";

  var table = document.getElementById("table");
  var row = table.insertRow(-1);
  var date = row.insertCell(0);
  var desc = row.insertCell(1);
  var amt = row.insertCell(2);
  date.innerHTML = document.getElementById("date").value;
  desc.innerHTML = document.getElementById("desc").value;
  amt.innerHTML = document.getElementById("amount").value;

  return false;
}
table {
  display: none;
}
<h1>Expense Tracker</h1>

<form action="">
  <label for="date">Date: </label>
  <input type="date" id="date">
  <br>

  <label for="desc">Description: </label>
  <input type="text" id="desc">
  <br>

  <label for="amount">Amount: </label>
  <input type="text" id="amount">
  <br>

  <input type="button" id="submit" value="Submit">
  <br>
</form>

<table id="table">
  <tr>
    <th>Date</th>
    <th>Description</th>
    <th>Amount</th>
  </tr>
</table>

Upvotes: 2

Related Questions