-2

I have used the following code to generate a dynamic table. Now I want to store those values in array so I can access and process them. How to do that?

P.S- Sorry for newbie question. I am just starting out java script.

$(document).ready(function(){
    $(".add-row").click(function(){
        var iorder = $("#iorder").val();
        var idate = $("#idate").val();
        var iamount = $("#iamount").val();
        var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + iorder + "</td><td>" + idate + "</td><td>" + iamount + "</td></tr>";
        $("table tbody").append(markup);
    });
Nirjhar
  • 9
  • 6
  • Does this answer your question? [javascript;storing values in array](https://stackoverflow.com/questions/9951500/javascriptstoring-values-in-array) – Mate Mrše Feb 27 '20 at 11:43
  • This is jQuery. – Mechanic Feb 27 '20 at 11:44
  • @MateMrše That's about storing value. I am looking to store values from a dynamically generated table. And, yes, forgot to mention, it's jquery. I am learning by building something. :) – Nirjhar Feb 27 '20 at 12:14

1 Answers1

0

You could have a global array inside your code which gets populated with the row data when you add the row to the table like this:

let data = [];

$(document).ready(function(){
    $(".add-row").click(function(){
        var iorder = $("#iorder").val();
        var idate = $("#idate").val();
        var iamount = $("#iamount").val();
        var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + iorder + "</td><td>" + idate + "</td><td>" + iamount + "</td></tr>";
        $("table tbody").append(markup);
        addToArray($("#iorder").val(), $("#idate").val(), $("#iamount").val());
    });

function addToArray(orderInput, dateInput, amountInput) {
 let temp = {
  order: orderInput,
  date: dateInput,
  amount: amountInput
 }
 data.push(temp)
}

This will add the row data as a object with 3 fields to the global array data.

Supporterino
  • 181
  • 9