N2J
N2J

Reputation: 175

Get Row column text 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.

Upvotes: 0

Views: 687

Answers (1)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

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();

Upvotes: 4

Related Questions