0

Just want to get the table row data and pass it. In my code it seems that it only getting first row data.

jsfiddle

$(".edit").click(function(){
    var tableData = $('tr.table').children("td").map(function() {
        return $(this).text();
    }).get();

  $('#test1').val($.trim(tableData[0]));
  $('#test2').val($.trim(tableData[1]));
  $('#test3').val($.trim(tableData[2]));
})
1
  • It's a bit unclear what you want. I assume you want to get the row the clicked .edit element is contained in? $('tr.table').children("td") gets all td element of the whole table. Commented Nov 29, 2013 at 8:34

2 Answers 2

1

Try

fiddle Demo

var tableData = $(this).closest('tr.table').children("td").map(function () {
    return $(this).text();
}).get();


Change

$('tr.table').children("td") 

//will get all tr.table and it's children td

to

$(this).closest('tr.table').children("td") 

//this will get closest tr to the current edit button


.closest()

Sign up to request clarification or add additional context in comments.

2 Comments

Please explain the problem and your solution, do not only post code.
@FelixKling Done Sir .I was updating answer.
1
$(".edit").click(function(){
    var tableData = $(this).closest("tr").children("td").map(function() {
     // by $(this) go to current td then by closest go its closest "tr" then find their td        
        return $(this).text();
    }).get();

  $('#test1').val($.trim(tableData[0]));
  $('#test2').val($.trim(tableData[1]));
  $('#test3').val($.trim(tableData[2]));
});

update fiddle

reference closest()

1 Comment

Please explain the problem and your solution, do not only post code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.