admin管理员组文章数量:1344941
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>
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.
Share Improve this question edited Oct 22, 2020 at 13:53 Derek Wang 10.2k4 gold badges19 silver badges40 bronze badges asked Oct 22, 2020 at 13:48 Yesha MajmundarYesha Majmundar 491 gold badge2 silver badges7 bronze badges2 Answers
Reset to default 6The 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>
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>
本文标签: Insert form values in a table using javascriptStack Overflow
版权声明:本文标题:Insert form values in a table using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743779713a2537608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论