N2J N2J - 6 months ago 17
jQuery Question

Get text of each row and its column using jquery

I trying to get the text of input fields of each row and its columns on a save click button. Rows can be more than one. I want all the texts of all the rows and their respective columns.I don't know how to put it in a loop to get all the inputs values of more than one row. Below is what i tried to get only the one row input value and I am getting undefined :

$('#btnSave').click(function() {
$("#tab_logic").find('tr').each(function() {
if ($(this).find('input[type="text"]')) {
var data1 = $(this).find('td:eq(0):input[type="text"]').val();
alert(data1);
}
});
});


Any help would be very much appreciated. Thanks in Advance.

Answer

Loop through each td and then check if it has any text field using the length peroperty,

$("#tab_logic tr td").each(function() {
  if ($(this).find('input[type="text"]').length > 0)) {
    var data1 = $(this).find('input[type="text"]').val();
    alert(data1);
  }
});

To Get the values into an array, use

var arr = $("#tab_logic tr td input[type='text']").map(function() {
   return $(this).val();
}).get();