0

I have a table in which I have editable data. Each 'editable' section covers two table rows.

<table>
    <tr data-doc_id2='doc_id2'>
    </tr>
    <tr></tr>
    <tr data-doc_id2='doc_id2'>
    </tr>
    <tr></tr>
    <tr data-doc_id2='doc_id2'>
    </tr>
    <tr></tr>
    <tr data-doc_id2='doc_id2'>
    </tr>
    <tr></tr>
</table>

(Please note I haven't put the <td></td> in....)

My jQuery so far is as follows. This will replace the first row with the relevent ID. I am having trouble selecting the second row... next() seems to be looking for the next row that has the ID.

 var new_row="<tr data-doc_id2='"+doc_id+"'>
    <td>"+doc_name+"</td>
    <td>"+first_ins4+"</td>
    <td>"+job_price4+"</td>
    <td>"+tender4+"</td>
    <td><a class='edit_docs' href='javascript:void(0);'>Edit</a> | <a class='del_doc' href='javascript:void(0);'>Delete</a></td>
 </tr>";
    var new_row2="<tr><td colspan='4'>"+second_ins4+"</td><td></td></tr>";

    var row_to_change=$("tr:[data-doc_id2='"+doc_id+"']");
    $(row_to_change).replaceWith(new_row);
2
  • You've swapped the - and _, added a 2, and prepended a : to the selector, but that shouldn't select the first row either. Commented Sep 22, 2012 at 14:50
  • Sorry - the html was more of an example html as the original table is dynamically generated... I will look at this Commented Sep 22, 2012 at 14:56

1 Answer 1

2
 var row_to_change=$("tr:[data-doc_id2='"+doc_id+"']");
 $(row_to_change).closest('tr').next('tr').remove();
 $(row_to_change).replaceWith(new_row);

Instead of having new_row and new_row2 I stuck the HTML together in new_row variable replacing one of the rows with the two new rows then removed the next row by using a standard selector. Clumsy but works.

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

1 Comment

If row_to_change is a <tr>, then you don't need .closest("tr").

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.