JavaScript Sort table rows based on background-color using jquery sort table rows javascript,sort table rows matlab,sort table

This is my FIDDLE

<table>
    <tr>
        <td>
            <div style="background:blue;color:white">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:pink;color:white">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:blue;color:white">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:green;color:white">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:pink;color:white">hello</div>
        </td>
    </tr>
     <tr>
        <td>
            <div style="background:green;color:white">hello</div>
        </td>
    </tr>
</table>

How do i rearrange the html table rows based on color? I want to group html table rows based on the background color of the rows.

Answer:1

Use sort() to sorting array of tr elements. You can get backgroud-color of element in function of sort and set arrangement of every element.

$("table tr").sort(function (a, b){
    return $("div", b).css("background") < $("div", a).css("background") ? 1 : -1;    
}).appendTo('table');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <td>
            <div style="background:blue">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:pink">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:blue">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:green">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:pink">hello</div>
        </td>
    </tr>
    <tr>
        <td>
            <div style="background:green">hello</div>
        </td>
    </tr>
</table>
Answer:2

I reviewed your question, I think you want to differentiate each tr by there color, adding html, style and script for you here.

Here is the Html

 <table>
     </tbody>
        <tr><td>123</td></tr>
        <tr><td>123</td></tr>
        <tr><td>123</td></tr>
        <tr><td>123</td></tr>
        <tr><td>123</td></tr>
     </table>

Do add this script, by this function all your tr will have unique classes. you can add there background colors etc style on the base of class

 <script>
 // please do add jQuery file to prevent error
 function adddClass() {
      for(i=1; i<=6; i++) {
          alert("");
          jQuery('table tr:nth-child('+ i +')').addClass("color"+i);
      }
  }
  adddClass();
  </script>

Here is the style for background color of each table row tr

<style>
.color1{background-color:orange;}
.color2{background-color:teal;}
.color3{background-color:red;}
.color4{background-color:#717171;}
.color5{background-color:khaki;}
.color6{background-color:lightgray;}
tr, table, body{width:100%;}
</style>

Hope this will help, Thanks.!

Answer:3

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...

Is it possible in js to create a date object that is not affected by timezone? For example i want to set the year "2015", the month "03" and the day "31". var ex = new Date(2015,3,31); If i "print" ...

Is it possible in js to create a date object that is not affected by timezone? For example i want to set the year "2015", the month "03" and the day "31". var ex = new Date(2015,3,31); If i "print" ...

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

  1. dynamic jquery dialog
  2. jquery dynamic dialog box example
  3. jquery dynamic dialog example
  4. dynamic jquery ui dialog
  5. jquery dynamic modal dialog
  6. create dynamic dialog jquery
  7. jquery dynamic confirm dialog
  8. dynamic content in jquery dialog
  9. dynamic buttons dialog jquery
  10. jquery dialog dynamic width
  11. jquery dialog dynamic title
  12. jquery dialog dynamic height
  13. jquery dialog dynamic button text
  14. jquery dialog dynamic text
  15. jquery dialog dynamic size
  16. jquery dialog dynamic html
  17. jquery dialog dynamic position
  18. jquery dialog dynamic div
  19. jquery dialog dynamic message

In Javascript (in Chrome devtools console panel and Node.js v0.12.5), I'm getting an incorrect answer for the product of these two large numbers: input: 41962049 * 1827116622 output: ...

In Javascript (in Chrome devtools console panel and Node.js v0.12.5), I'm getting an incorrect answer for the product of these two large numbers: input: 41962049 * 1827116622 output: ...