How to highlight alternate table row using jQuery


Use the jQuery :nth-child() selector

You can use the jQuery :nth-child() selector to create the zebra striped table by highlighting its alternate rows. The :nth-child(N) selector accepts these arguments — the index of each child to match (starting with 1), the string even or odd, or an expression (e.g. :nth-child(2):nth-child(even):nth-child(2n). Let's take a look at an example:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Zebra Striped Table with jQuery</title>
<script src=""></script>
        // Apply alt class on alternate table row
        $("table tbody tr:nth-child(2n)").addClass("alt");
        margin: 30px;
        border-collapse: collapse;
    table tr{
        border-bottom: 1px solid #666;
    table th, table td{
        padding: 10px;
    /* Highlighter class */
        background: #cdcdcd;
                <th>First Name</th>
                <th>Last Name</th>