0

Is there a way to set background color conditionally ony when data is present?

Only when someone sets a time due it shows up in the column and I want to make that more visible.

I am familiar with setting a condition based on .innerHTML= but is there way to express if a value is present apply background and if not do nothing.

Edit 1

I am using this code in a script editor currently. I want to also be able to highlight a cell under the "Time" column when it is not blank.

<script type="text/javascript" language="javascript">
  var x = document.getElementsByTagName("TD");
  var i=0;
  for (i=0;i<x.length;i++)
  {

    if (x[i].className=="ms-vb2") 
    {

      if (x[i].innerHTML=="Request")
      {
        x[i].style.backgroundColor="#ed2a2a"; 
      }

      if (x[i].innerHTML=="Assigned")
      {
        x[i].style.backgroundColor="Yellow"; 
      }

      if (x[i].innerHTML=="Arrived to Modality")
      {
        x[i].style.backgroundColor="Green"; 
      }

     }

   }

</script>

1

You can use the following code to achieve it.

<script type="text/javascript">

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

   SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
     OnPostRender: function(ctx) {
       var rows = ctx.ListData.Row;
       for (var i=0;i<rows.length;i++)
       {
          var rowId = GenerateIIDForListItem(ctx, rows[i]);
          var row = document.getElementById(rowId);
          if(rows[i]["Followup_x0020_Date"] != "")
          {
            row.style.backgroundColor = "#dff0d8";
          }


       }
     }
   }); 

});

</script>

Note: you need to change "Followup_x0020_Date" to your Date column.

The result is: enter image description here

  • Thank you for the reply. I to combine what you provided me with the code I was already using but it did not work. Nor did it work when just using the code you provided alone. – Jason D Jun 2 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.