How to shift a table column

How to shift a table column

Hi,

I'm new to JQuery and also JavaScript so I'm running into a lot of pitfalls and watched a lot of examples (learning by doing). What I want to do is to create a function which shifts a table column one position to left. Therefore I'am trying to select all relevant td and th elements. My problem is to rearange the elements. Within the both each calls, see the code below, $(this).get(i) would result the td respectively the th element where the before function is undefined. But how to insert the elements correctly or is this the wrong way?

$.fn.shiftLeft = function(col) {
   // Make sure col has value
   if(!col){ col = 1; }
   var trsToMove = $('tr td:nth-child('+col+')',this);
   var thsToMove = $('tr th:nth-child('+col+')',this);

   var trsBefore = $('tr td:nth-child('+(col -1)+')',this);
   var thsBefore = $('tr th:nth-child('+(col -1)+')',this);

   thsBefore.each(function(i,ob) {
      $(this).before(thsToMove.get(i));
   });

   trsBefore.each(function(i,ob) {
      $(this).before(trsToMove.get(i));
   });
}


Regards
Michael