Danka
Danka

Reputation: 291

How to iterate through each row and collect data

Rows (id='row_property_'+number) in table contain four columns :

  1. select element (id='multiple_object_type_'+number)
  2. input type="text" (id='multiple_instance_id_'+number)
  3. nested table (id='table_properties_'+number)
  4. input type="button"

How to iterate through each row and collect data in two arrays from select and input with jquery ?

Upvotes: 1

Views: 563

Answers (2)

Richard Dalton
Richard Dalton

Reputation: 35793

var selectArray = $('table tr td > select').map(function() {
    return $(this).val();
}).get();

var inputArray = $('table tr td > input:text').map(function() {
    return $(this).val();
}).get();

This might do what you want.

JSFiddle Example

Upvotes: 0

Thomas Clayson
Thomas Clayson

Reputation: 29925

Ok, add classes to the select and input elements so that the table looks something like this:

<table id="myTable">
  <tr>
    <td><select class="rowSelect"></select></td>
    <td><input type="text" class="rowInput" /></td>
    ... etc ...
  </tr>
</table>

Then you can get the values from each row like this in jquery:

$(function(){
  $('#myTable tr').each(function(){
    alert('select value is '+$(this).find('select.rowSelect'));
    alert('input value is '+$(this).find('input.rowInput'));
  });
});

Upvotes: 1

Related Questions