How to display alternate color in table using Jquery Selector


jqueryselector

While working with asp.net mvc project, we will get scenario to display the alternate color of tabular grid. We can do this task using Jquery selector in very easy way like this


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
     <script src="../Scripts/jquery.min.js"></script>
    <script>
        $(function () {
            $("tr:odd").css("background-color", "yellow");
        });
    </script>
</head>
<body>
    <fieldset>
        <legend><b>Alternate table row color using Jquery selector</b></legend>
    
    <table border="1">
        <tr>
            <th>EmpName</th>
            <th>City</th>
            <th>Contact Num</th>
        </tr>
        <tr>
            <td>Ram</td>
             <td>Bangalore</td>
            <td>1234567890</td>
        </tr>
         <tr>
            <td>Hari</td>
             <td>Bangalore</td>
            <td>1234567890</td>
        </tr>
         <tr>
            <td>Mohan</td>
             <td>Bangalore</td>
            <td>1234567890</td>
        </tr>
         <tr>
            <td>Rohan</td>
             <td>Bangalore</td>
            <td>1234567890</td>
        </tr>
    </table>
        </fieldset>
</body>
</html>

Note: in above example we have used $(“tr:odd”) as Selector

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: