Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to select all even/odd rows in table using jQuery ?

  • Last Updated : 12 Aug, 2021

In this article, we will see how to make a table by selecting the alternate rows i.e. selecting the even or odd rows by clicking on the respective buttons. This feature can be useful at the time of selecting the specific data/elements of either of the rows or to highlight the table of data for display from the large data set. This enables us to quickly review the table at a glance. We can achieve the same by selecting the odd or even rows using tr: odd or tr: even selectors in the table. The row index starts from 0 in the HTML tables. We will use jQuery for selecting either of the rows by clicking the button. 

In order to use jQuery in the HTML file, we will add the below syntax inside the <head> tag.

<script src=""

Syntax for selecting odd rows:

$("table tr:odd").css({"property":"value",
                           "property": "value"});

Syntax for selecting even rows:

$("table tr:even").css({"property":"value",
                           "property": "value"});



  • Design a table that will contain the data. Use the required styling properties to highlight the different rows in order to differentiate the row data.
  •  Use “tr: odd” to select the odd data & use “tr: even” to select the even data in the row.

Example: In this step, we will create a table having the table row & table column & will use the above all syntax for the selection of rows. 


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge" />
    <meta name="viewport" 
                   initial-scale=1.0" />
    <title>Geeks for Geeks</title>
    <!-- Including jQuery  -->
      h1 {
        color: #006600;
      button {
        color: white;
        background-color: #006600;
        width: auto;
        height: 30px;
      body {
        text-align: center;
      div {
        margin: 10px;
        height: auto;
        width: auto;
        position: relative;
        display: flex;
        justify-content: center;
        text-align: center;
        display: flex;
      td {
        border: 5px solid #006600;
        border-collapse: collapse;
        padding: 10px;
      thead {
        color: #006600;
        font-size: 20px;
        font-weight: bold;
    <h1>Geeks For Geeks</h1>
    <button id="odd">ODD ROWS</button>
    <button id="even">EVEN ROWS</button>
      $(document).ready(function () {
        $("#odd").click(function () {
          $("table tr:odd").css({
            "background-color": "#00e673",
            color: "white",
        $("#even").click(function () {
          $("table tr:even").css({
            "background-color": "#b3b3cc",
            color: "white",


Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!