Jquery

Alternate Row Color Using jQuery

In this post, you will learn how to create alternate row colors in an HTML table with help of jQuery, Since the table has tr which are representing rows, so we need to change the background-color CSS property of tr in the table. We can also do the same using the CSS as well.

Jquery is a very power full library for web development, we just use add CSS class method $(“tr:even”).css of jquery to alternate table row colors. We require jquery lib for alternate row color of table.

Below are step to alternate row color

Step 1: Include JQuery library

Related Post
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


Step 2: Create a sample table in the page.

<table>
 <tbody>
  <tr>
   <th>Month</th>
   <th>Savings</th>
  </tr>
  <tr>
   <td>Feb</td>
   <td>$100</td>
  </tr>
  <tr>
   <td>March</td>
   <td>$100</td>
  </tr>
  <tr>
   <td>April</td>
   <td>$100</td>
  </tr>
  <tr>
   <td>May</td>
   <td>$100</td>
  </tr>
 </tbody>
</table>

Step 3: Include below code for alternate color for div as well as a row of a table.

<script>
$(document).ready(function()
{
  //for div
  $("div:odd").css("background-color", "#F4F4F8");
  $("div:even").css("background-color", "#EFF1F1");

  //for table row
  $("tr:even").css("background-color", "#F4F4F8");
  $("tr:odd").css("background-color", "#EFF1F1");
});
</script>

Demo & Download Code

Please feel free to send queries to me using below comment section.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories