1
<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);
}

1
  • You are declaring addArray and assigning it the value of [] in the addRecord method. This will clear the array and push an add.value every time it fires. Define addArray outside of that method. Commented Oct 5, 2017 at 12:51

2 Answers 2

6

a 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>

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

Comments

1

"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>

1 Comment

This helps. Thank you!

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.