Jenny
Jenny

Reputation: 919

How can I move a table row from one table to another?

I have a table row without an ID/Class that needs to be moved to the bottom of another table, please see JSfiddle

The following needs to be moved into another table above the last row in that table See complete code here: FIDDLE

<table class="BBFormTable DonationFormTable">
    <tbody>
        <tr>
            <td class="BBFieldCaption DonationFieldCaption">
                <label id="PC1567_lblLgnCtl195" for="PC1567_195">Additional Honor/Memorial Name:</label>
            </td>
            <td class="taLeft" id="PC1567_ParentControl195"><input type="text" class="BBFormTextbox   LoginFormTextbox" id="PC1567_195" maxlength="250" name="PC1567$195"></td>
        </tr>
    </tbody>
</table>

Upvotes: 0

Views: 105

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388406

If you can't depends on the ids then try something like

jQuery(function ($) {
    $('.BBFormTable.DonationFormTable > tbody:last > tr:last').before($('tr').has('> td > label:contains("Additional Honor/Memorial Name:")'))
})

Demo: Fiddle

Upvotes: 0

Palpatim
Palpatim

Reputation: 9272

Try this:

var $movingRow = $('#PC1567_lblLgnCtl195').parents('tr');
if ($movingRow) {
    var $destinationRow = $('#PC1567_chkAcknowledge').parents('tr');
    $movingRow.insertAfter($destinationRow);
}

http://jsfiddle.net/Palpatim/yFtC7/3/

The trick is to latch onto an ID near the element you want to use, and navigate around from there using jQuery's helper functions.

Make sure to spend some time reading through the jQuery documentation. Some of the functions, especially for DOM manipulation, take some careful reading (insert and insertAfter, for example, may behave differently than you expect).

One nice thing about jQuery: as long as there's a deterministic ID somewhere in your selector path, you can usually navigate around. It's definitely not the most efficient way of doing things, and it's brittle (if your structure changes, or your weird IDs change, your script will have no effect), but this should at least give you some ideas to get started with.

Upvotes: 1

Related Questions