admin管理员组文章数量:1290950
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
I need to take input from the user, when user clicks on the Add button after entering data in the Textbox. User may add 'n' number of inputs which I do not know. I need to store the inputs in an array and display it on clicking the Display button. How should that be done? I tried using push(), however it doesn't work:
function addRecord()
{
var add= document.getElementById("add");
var addArray= [];
addArray.push(add.value);
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
I need to take input from the user, when user clicks on the Add button after entering data in the Textbox. User may add 'n' number of inputs which I do not know. I need to store the inputs in an array and display it on clicking the Display button. How should that be done? I tried using push(), however it doesn't work:
function addRecord()
{
var add= document.getElementById("add");
var addArray= [];
addArray.push(add.value);
}
Share
Improve this question
edited Oct 5, 2017 at 13:02
F0XS
1,2693 gold badges16 silver badges19 bronze badges
asked Oct 5, 2017 at 12:46
GeorgeGeorge
532 gold badges3 silver badges10 bronze badges
1
-
You are declaring
addArray
and assigning it the value of[]
in theaddRecord
method. This will clear the array and push anadd.value
every time it fires. DefineaddArray
outside of that method. – glhrmv Commented Oct 5, 2017 at 12:51
2 Answers
Reset to default 6a spaghetti solution might be as below snippet,
values = [];
function addRecord() {
var inp = document.getElementById('inputtext');
values.push(inp.value);
inp.value = "";
}
function displayRecord() {
document.getElementById("values").innerHTML = values.join(", ");
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
<div id='values'></div>
"use strict";
let addButton = document.querySelector('#add');
let displayButton = document.querySelector('#display');
let records = [];
addButton.addEventListener('click', addRecord);
displayButton.addEventListener('click', displayAll);
function addRecord() {
let record = document.querySelector('#inputtext').value;
if (!record) {
return;
}
records.push(record);
document.querySelector('#inputtext').value = '';
}
function displayAll() {
alert(records);
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add">Add </button>
<button type="button" id="display">Display</button>
</td>
</tr>
</table>
本文标签: htmlJavaScript store user input in an array on button clickStack Overflow
版权声明:本文标题:html - JavaScript store user input in an array on button click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741517317a2382972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论