Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can make use of javascript object oriented concept</p> <p>Each table has its corresponding table object, so does its rows and cells. With this technique, every object has its "element" in the real DOM.</p> <p>The trick is, attach the event listener (e.g. when user types something in the textbox) to the object, so that that event listener will only correspond to that specific cell, not messing up with other cells in other column, row, and tables.</p> <p>Here is possible solution. Sorry for my jQuery heavy dependent. I am just not sure how to create element with document.createElement() (my bad knowledge), you can of course change them with pure javascript implementation (no jQuery). </p> <pre><code> values=new Array(); function Cell(id,values_row){ //Cell class this.id=id; this.element=$("&lt;td&gt;&lt;/td&gt;"); this.textbox=$("&lt;input type='text' name='"+this.id+"' id='"+this.id+"'&gt;&lt;/input&gt;"); values_row[id]="" //initiate default value == "" //here is the trick this.textbox.change(function(){ //you can use onchange if you dont like jQuery values_row[id]=$(this).val(); //other tasks need to be done when user changes the value of a textbox (typing) }); this.textbox.appendTo(this.element); } function Row(id,number_of_columns,values_table){ //Row class this.id=id this.element=$("&lt;tr&gt;&lt;/tr&gt;") values_table[id]=new Array(); //make array "values" becomes 3D for(var col =0;col&lt;number_of_columns;col++){ var the_cell=new Cell(col,values_table[col]); the_cell.element.appendTo(this.element); } } function Table(id,number_of_columns){ //Table class this.id=id this.element=$("&lt;table&gt;&lt;/table&gt;") values[id]=new Array(); //make array "values" becomes 2D for(var row=0;row&lt;4;row++){ var the_row=new Row(row,number_of_columns,values[id]); the_row.element.appendTo(this.element) } } // creating the tables for(var t=0;t&lt;number_of_tables_needed;t++){ the_table=new Table(t,number_of_column_for_this_table); the_table.element.appendTo(table_container_element); } //do input some text into some cells then alert(values) </code></pre> <p>The alert will display a 3D array containing the values of all cells indexed by table number, row number, and of course the column that points exactly to a cell. The default value for cell's value is ""</p> <p>With this technique, you do not need find which the correct array element for a corresponding cell whose value just been inputted. Instead, attach the event listener prior to the element creation in DOM.This is an object oriented way / approach to the problem.</p> <p>Rather than having O(n^3) algorithm complexity when you need to find the corresponding array cell each time user makes an input, this solution is a taking a bit more time during object initialization, but afterwards, it is O(1).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload