Javascript in SharePoint CEWP not working

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>

Answer

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>

Leave a Reply

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