• Highlight SharePoint List rows conditionally using jQuery

    by  • April 15, 2010 • MOSS 2007 • 17 Comments

    In this article we will see how to Change the row Background color based on the value of a column in a sharepoint list and we will be doing it using  jQuery.

    Below is a screenshot of a Task Tracker whose rows are highlighted based on the percentage complete.

    Here is how you can do it.

    1. Create   Sharepoint list
    2. Edit allitems.aspx page and add a content editor web part and copy the below jQuery code to that webpart.

    <script type="text/javascript">
    $(document).ready(function(){
    $Text = $("td .ms-vb2:contains('Not Started')");
    $Text.parent().css("background-color", "#461B7E");
    var myelement = $Text.parent().parent();
    $Text = $("td .ms-vb2:contains('Completed')");
    $Text.parent().css("background-color", "#4CC417");
    $Text = $("td .ms-vb2:contains('In Progress')");
    $Text.parent().css("background-color", "#EAC117");
    });
    </script>
    

    About

    Hojo Clement was working in US Technology in Trivandrum as a Senior Software Engineer. He has 6 years experience in working with Web Development Technologies inlcuding MOSS and ASP.net.

    17 Responses to Highlight SharePoint List rows conditionally using jQuery

    1. Toya Crawley
      May 10, 2010 at 1:45 pm

      Is there a step missing?? I created a Test Task List in MOSS 2007 and added the CEWP and there is no color displayed in the rows. Is there something missing after creating the list and before adding the CEWP webpart?? I need something like this to work before Wednesday.
      Thanks
      Toya

    2. May 11, 2010 at 5:10 am

      Hi Toya

      There was some extra code in our source code. We have removed it. Please use our updated code above and see if it works.

      Shoban

    3. cy
      May 20, 2010 at 5:31 pm

      Does not work

    4. May 20, 2010 at 6:07 pm

      Hi CY

      I have emailed you. Let us know ur issue and we can work it out

    5. Honari
      July 7, 2010 at 4:00 am

      Hello,
      I did the same thing you said here but it has no effect on my pages.
      and also I want to change the condition to when we put a check next to a field, the row
      changes its color.
      Thanks.

    6. August 9, 2010 at 4:30 pm

      I have had great success with this, but now want to change the colour of the text too.
      Can you suggest code to do that too?

      • Hojo Clement
        August 11, 2010 at 6:27 am

        You can change color using below code

        $Text.parent().css(“color”, “#ffffff”);

    7. alexmiami
      September 16, 2010 at 9:01 pm

      Shoban
      Hi, I’m trying to the same and I add the script to the CEWP nothing happens. what is missing. Thanks in advance
      Alex

    8. September 17, 2010 at 9:26 am

      @alexmiami

      Did you change the ID of the texbox?

    9. Mark
      November 24, 2010 at 10:30 am

      Hi

      I’ve tried adding this to a list that we have on our MOSS 2007 site. It isn’t working, although other scripts on the page do.

      My list has a column called Status, and I’ve amended the script to take the values that we use into consideration.

      Where would I find the ID of the text box? Also, does this need adding at the top or bottom of the page? Other scripts are below the main list web part, which is where I’ve added this one. I’ve also tried it above the list WP.

      Any suggestions please..

      Ta

    10. November 29, 2010 at 7:23 am

      Mark

      Add the code to the botto is the page. You can get the ID of the text box by viewing the source code of the page in a webbrowser.

    11. Mark
      November 29, 2010 at 4:23 pm

      Thanks for the reply

      $(document).ready(function(){
      $Text = $(“td .ms-vb2:contains(‘Live’)”);
      $Text.parent().css(“background-color”, “#461B7E”);
      var myelement = $Text.parent().parent();
      $Text = $(“td .ms-vb2:contains(‘Complete’)”);
      $Text.parent().css(“background-color”, “#4CC417″);
      $Text = $(“td .ms-vb2:contains(‘Future Work’)”);
      $Text.parent().css(“background-color”, “#EAC117″);
      });

      And the code from the source view

      Status

      I’ve tried changing the field ID to ms-vh2, but still no joy.

      Thanks

    12. Mark
      November 29, 2010 at 4:24 pm

      Edit to above. Not sure how the comment will display for you, but for me, it’s now displaying ‘Status’ as a hyperlink to Javascript: which is the correct column.

    13. Mouldy
      December 11, 2010 at 8:38 pm

      Bollocks – You might as well remove this page. There’s enough crap on the internet OR update it with proper instructions

    14. Bonnie
      June 4, 2011 at 4:30 pm

      You need to add the “call function” for the library to get the jquery to work. Add this at the top:

    15. June 18, 2011 at 5:17 pm

      Hi,

      It is not working, I have followd the steps which you mention, but still no use.
      What should I do ?

      Thanksm
      Srikanth Reddy

    16. Prashant
      November 23, 2011 at 7:49 am

      Its working fine for conditional formatting but i dont want to put any condition . I want the color for all the field for a specific column

    Leave a Reply

    Your email address will not be published. Required fields are marked *