If you want to highlight alternate table row using jQuery? Use the jQuery :nth-child() selector to create the zebra striped table by highlighting its alternate rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Zebra Striped Table with jQuery</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script> $(document).ready(function(){ // Apply alt class on alternate table row $("table tbody tr:nth-child(2n)").addClass("alt"); }); </script> <style> table{ margin: 30px; border-collapse: collapse; } table tr{ border-bottom: 1px solid #666; } table th, table td{ padding: 10px; } /* Highlighter class */ .alt{ background: #cdcdcd; } </style> </head> <body> <table> <thead> <tr> <th>Row</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td>Carter</td> <td>johncarter@mail.com</td> </tr> <tr> <td>2</td> <td>Peter</td> <td>Parker</td> <td>peterparker@mail.com</td> </tr> <tr> <td>3</td> <td>John</td> <td>Rambo</td> <td>johnrambo@mail.com</td> </tr> <tr> <td>4</td> <td>Harry</td> <td>Potter</td> <td>harrypotter@mail.com</td> </tr> <tr> <td>5</td> <td>Percy</td> <td>Jackson</td> <td>percyjackson@mail.com</td> </tr> </tbody> </table> </body> </html> |
Output
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.