archytect
archytect

Reputation: 3715

using jQuery to swap columns in a table

What's the best way, with jQUery to swap columns in a table?

 <table>
      <tr>
           <td class="x"></td>
           <td class="y"></td>
      </tr>
      <tr>
           <td class="x"></td>
           <td class="y"></td>
      </tr>
      <tr>
           <td class="x"></td>
           <td class="y"></td>
      </tr>
 </table>

I'm using a third party plugin that generates the table and it's not very flexible, I'd like to shift the column with a class of y over as the first column and the column with a class of x as the second. How can this be achieved?

Upvotes: 0

Views: 1749

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

Iterate each element with class y and insertBefore the previous element in your case.

$('.y').each(function(i,ele){
    $(ele).insertBefore($(ele).prev()) 
});

Upvotes: 4

Related Questions