0

I wrote a simple javascript to change the color of an html class based on its textcontent/innerHTML. When writing the script, it works fine when I put it directly in to the developer tools console (F12) for Chrome. But when I try to call the script from a CEWP, it doesn't work. What am I missing? Here is the html I embedded in the CEWP. Long time administrator, first time diving into CSOM development. I'm sure this is something extremely simple but I am at a loss..

<script type="text/javascript">
var status_array =document.getElementsByClassName("sefl_status");
var pattern = new RegExp("Effective");
for (i=0; i < status_array.length; i++)
{
    if (pattern.test(status_array[i].innerHTML)===true)
    {
    status_array[i].style.color="green"
    }
};
</script>
sp_vennem
  • 55
  • 1
  • 8

1 Answers1

0

I modify the code as below for your reference:

<script type="text/javascript">
window.onload=function(){
    var status_array =document.getElementsByClassName("sefl_status");
    var pattern = new RegExp("Effective");
    for (var i=0; i < status_array.length; i++)
    {
        if (pattern.test(status_array[i].innerHTML)===true)
        {
            status_array[i].style.color="green";
        }
    }
}
</script>
LZ_MSFT
  • 4,079
  • 1
  • 8
  • 9
  • Thanks! It works now but inconsistently. A little more background, this is for a custom search results page in SharePoint 2013 (soon to be 2016). The customer wanted certain managed properties to be visible in the search results.. one being if a doc is Effective or Rescinded. I then enabled search navigation to toggle between Effective, Rescinded, and All Docs configured by custom results sources on each page. The script works fine in the initial query but not when I toggle between the search navigation. – sp_vennem Mar 29 '18 at 15:26
  • I moved the CEWP to the bottom of the page and it is still having the same issue but appears to be a little more reliable.. – sp_vennem Mar 29 '18 at 15:55
  • I realized that I need to run in the display template html..having issues kicking that off.. – sp_vennem Mar 29 '18 at 19:10
  • Yes, In search result page, you need add to display template. Or you can also use js setIntervel method to solve this issue. – LZ_MSFT Mar 30 '18 at 05:10
  • Definitely on the right track. I got it to load from the display template, there is a slight delay however, but I am marking your response as an answer! Thank you so much for your help! – sp_vennem Mar 30 '18 at 16:15