0

How can i use jQuery to reverse all the tr except first and last ones.

I have seen several examples on how to reverse them all , but cant figure out how to incorporate any of those to specify all by first and last ones.

example html marked up below

    <table class="homepagemodule report" id="league_chat" align="center" cellspacing="1">
      <caption>
        <span href="javascript:void(0);" style="visibility: visible;" class="module_expand" classname="module_expand">[-]</span>
        <span>League Chat <a href="javascript:chat_window('http://www15.myfantasyleague.com/2015/chat?L=65821&amp;COUNT=40');"><img src="http://www15.myfantasyleague.com/window-16x16.png" title="New Window" alt="New Window" border="0" height="16" width="16"/></a></span>
      </caption>
      <tbody>

<!-- DO NOT REVERSE FIRST TR BELOW -->
        <tr>
          <th>By</th>
          <th>Message</th>
        </tr>

<!-- REVERSE ALL TR HERE  -->
        <tr class="oddtablerow" classname="oddtablerow" title="Posted: Wed May 20 5:03:43 p.m. ET 2015" id="1432155823grk906">
          <td>Commissioner</td>
          <td>test</td>
        </tr>
        <tr class="eventablerow" classname="eventablerow" title="Posted: Sun May 3 10:05:04 a.m. ET 2015" id="1430661904spl184">
          <td>
            <b>Franchise 1</b>
          </td>
          <td>
            <b>test</b>
          </td>
        </tr>
        <tr class="oddtablerow" classname="oddtablerow" title="Posted: Sat May 2 6:59:25 p.m. ET 2015" id="1430607565cyo906">
          <td>Commissioner</td>
          <td>test</td>
        </tr>
        <tr class="eventablerow" classname="eventablerow" title="Posted: Sat May 2 6:59:19 p.m. ET 2015" id="1430607559qma757">
          <td>
            <b>Commissioner</b>
          </td>
          <td>
            <b>asdfasdf</b>
          </td>
        </tr>
        <tr class="oddtablerow" classname="oddtablerow" title="Posted: Sat May 2 6:59:14 p.m. ET 2015" id="1430607554imv113">
          <td>
            <b>Commissioner</b>
          </td>
          <td>
            <b>asdfasdf</b>
          </td>
        </tr>
        <tr class="eventablerow" classname="eventablerow" title="Posted: Sat May 2 6:59:08 p.m. ET 2015" id="1430607548soa621">
          <td>Commissioner</td>
          <td>asdfa</td>
        </tr>

<!-- DO NOT REVERSE LAST TR BELOW -->
        <tr classname="oddtablerow" class="oddtablerow">
          <td colspan="2">
            <form action="" name="chat"><input name="chat" id="chat_text_field" size="30" maxlength="200" type="text"/><input onclick="postChatMessage();" value="Post" type="button"/><a href="javascript:toggle_chat_audio();"><img id="chat_audio_icon" src="http://www15.myfantasyleague.com/sound-off-16x16.png" title="Chat Muted" alt="Chat Muted" border="0" height="16" width="16"/></a> <a href="javascript:document.chat.chat.value=':clear:'; postChatMessage();"><img src="http://www15.myfantasyleague.com/note-remove-16x16.png" title="Clear Messages" alt="Clear Messages" border="0" height="16" width="16"/></a> <a href="http://football15.myfantasyleague.com/2015/options?L=65821&amp;O=222" target="_blank"><img src="http://www15.myfantasyleague.com/mflicons/video.gif" class="videoconferenceicon" title="League Videoconference" alt="League Videoconference" border="0" height="11" width="16"/></a><br/><span id="visitor_count">1</span> leaguemates on-line: <span id="visitor_list">Commissioner</span><br/> Send Message To: <select size="1" name="TO_FID"><option value="">Everyone</option><option value="0000">Commissioner</option><option value="0001">Franchise 1</option><option value="0002">Franchise 2</option><option value="0003">Franchise 3</option><option value="0004">Franchise 4</option><option value="0005">Franchise 5</option><option value="0006">Franchise 6</option><option value="0007">Franchise 7</option><option value="0008">My Team</option><option value="0009">Franchise 9</option><option value="0010">Franchise 10</option><option value="0011">Franchise 11</option><option value="0012">Franchise 12</option></select></form>
          </td>
        </tr>
      </tbody>
    </table>
MShack
  • 642
  • 1
  • 14
  • 33

2 Answers2

2

Using :first and :last as well as reverse(), you can do

var table = $('table');
var rows = table.find('tr:not(:first):not(:last)');

$('tr:first').after(rows.get().reverse());

Notice, you have to use after() because append will simply add all rows reversed(including first row).

BEFORE

AFTER

Also, see jQuery reversing the order of child elements

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • it appears the site has a slight delay with their loading of the #league_chat table , how can i add a time delay for this to run ? – MShack May 20 '15 at 21:26
  • You will need to include this in the success callback of the loading of #league_chat table. Look in their documentation for using the callback or if you are using $.load() simply pass a success handler. Let me know if you need further help – AmmarCSE May 20 '15 at 21:29
  • AmmarCSE - i located this file script on their server which actually generates the league_chat table - I do have ability to edit this script - so can you look over and see what/where the edit would be to reverse the "tr" for the chat as its generated - except first and last rows ofc - https://jsfiddle.net/0fqcvyb9/5/ – MShack May 20 '15 at 21:33
  • @MShack, try inserting it at the very end of function parseChatXml. It should work there – AmmarCSE May 20 '15 at 21:38
  • well , tried everything possible and cant get it working right on the live site - http://www29.myfantasyleague.com/2015/home/18841?MODULE=MESSAGE20 – MShack May 20 '15 at 22:04
1

The examples that you have seen probably use this selector:

$('#league_chat tr')

being 'league_chart' your table's ID. All you need to do is change the selector to be:

$('#league_chat tr:not(:first-child):not(:last-child)')

By the way, I see that you added classes to each tr of your table to mark them as odd or even rows. This is not necessary, as you can retrieve your rows using the following selectors:

$('#league_chat tr:nth-child(odd)')
$('#league_chat tr:nth-child(even)')

So here is the full code (can't test it from here but should work):

var $firstTr = $('#league_chat tr:first-child');
var $reversedTrs = $('#league_chat tr:not(:first-child):not(:last-child)').reverse();
var $lastTr = $('#league_chat tr:last-child');
//erase all TRs
$('#league_chat').empty();
//append first TR
$('#league_chat').append($firstTr);
//append reversed TRs
$reversedTrs.each(function() {
  $('#league_chat').append($(this));
});
//append last TR
$('#league_chat').append($lastTr);
lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
  • so , how would i do not first child and not last child ? – MShack May 20 '15 at 21:15
  • Sorry, missed that the last one must also be discarded. Will update the answer right away. – lucasnadalutti May 20 '15 at 21:16
  • the classes are already in the existing HTML which i don't have access to change. Can you post your recommened code for the reversal as well, i've seen so many , and dating back to 2009 – MShack May 20 '15 at 21:19
  • You mean the full code for reversing? It will probably be the same as the examples you've seen, but no problem, will do. :) – lucasnadalutti May 20 '15 at 21:20
  • ty for assistance lucasnadautti - the chat table is generated slightly after the DOM - i posted the websites parseChatXML in another answer above , hope that can be altered a bit to get this working – MShack May 20 '15 at 21:36
  • You are welcome. Your code didn't work as it was, so I preferred not to modify it. I'm pretty sure that just adding the code I suggested to the end of your parseChatXML function should work, though. – lucasnadalutti May 20 '15 at 21:42