Click here to Skip to main content
15,881,803 members
Articles / Style
Article

Set table coloum background color on a condition using jquery

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 7.4K   1  
Normal 0 false false false EN-US X-NONE X-NONE

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

There are some projects which require to high light rows which matches the filter criteria in grid view or table, but there are other requirements where we have to highlight the entire column based on the column cell text/header text.

This functionality/requirement can be handled by user when user perform a click operation on column header or hover mouse on to the column or when user loads the data he would specify the filter expression to high light a specific column header value.

This can be handled through JavaScript also which requires a complex script to be written, but with JQuery you can write the script easily.

Here in this example, column “PF” is highlighted.

Following is the Script used to highlight the column.

 $(document).ready(function() {
            var pfColumn = $("#Grid_CoWorking th:contains('PF')");
            var index = $("#Grid_CoWorking th").index(pfColumn);
            $("#Grid_CoWorking td:nth-child(" + (index + 1) + ")").css("background-color", "red");
        });

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --