2

I have a simple table with an edit button at the end of each row as below:

<table class='document_table'>
<tr>
    <td>Item1</td>
    <td>Item2</td>
    <td>Item3</td>
    <td><a href='javascript:void(0);' class='edit_doc'>Edit</a></td>
</tr>
<tr>
    <td colspan='4'>Item4</td>
</tr>
<tr>
    <td colspan='4'>Item5</td>
</tr>
<tr>
    <td>Item1</td>
    <td>Item2</td>
    <td>Item3</td>
    <td><a href='javascript:void(0);' class='edit_doc'>Edit</a></td>
</tr>
<tr>
    <td colspan='4'>Item4</td>
</tr>
<tr>
    <td colspan='4'>Item5</td>
</tr>
</table>

What I need to do is when the edit button is clicked is populate a form with the data from the three rows. Note that the number of rows is dynamic.

Form html is as follows:

 <label>Input 1<label>
 <input type='text' class='input1' />
 <label>Input 2<label>
 <input type='text' class='input2' />
 <label>Input 3<label>
 <input type='text' class='input3' />...etc

So far my jQuery is as follows but is not working....

var row1=$(this).closest('tr').find('td').eq(0).val();
$('.input1').val(row1);

I also have to be alble to get the values from the next two rows :(

1 Answer 1

4

You should use .html() instead of .val() , like this:

var row1=$(this).closest('tr').find('td').eq(0).html();

To obtain your other rows, you could do:

var td = $(this).closest('tr').find('td');
var row1 = td.eq(0).html();
var row2 = td.eq(1).html();
var row3 = td.eq(2).html();
Sign up to request clarification or add additional context in comments.

5 Comments

Can't believe I've made such an elemtary mistake - the other problem though is obtaining the next two rows. Thank you :)
var td = $(this).closest('tr').next('tr').find('td'); seems to get the next table row
Yes, and what do you mean? you only wanted the values from the tr where the click happened, not from other tr's..
Sorry - should have made that clearer but you have helped with the overall problem. Thanks.
Just to clarify, values in row1, row2, row3 actually contain values of each CELL of one table row. Perhaps it would be better to name them cell1, cell 2, cell3. However, thanks for the answer!

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.