Jan 17, 2018

Update alternate div color using jQuery

We all are required to do some small stuff with jquery while we are doing the development in any kind of technology.
Today, we will learn how to update alternate div background class using javascript. 

It is very simple and easy to do it. For that, we have to write some lines of code as in below example. We have to take care of that, this code will execute once the page is fully loaded. For that, we have to use document.ready function.

Download sample code

Add below code at the end of your file.

$( document ).ready(function() {
  $(function(){
    $("div.post:odd").addClass('odd'); 
    $("div.post:even").addClass('even');   
  });
});
   
You can also watch below video for better understanding.
 

Top rated

no post found.