While programming in javascript, getting error "Cannot read property 'style' of null" please help me


#1

I have tried this solution, but its not working for me. referred : help me please with Uncaught TypeError: Cannot read property 'style' of null.

code I have written:

<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
   SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
     OnPostRender: function(ctx) {
       var statusColors =  {
          'Pending' : '#9ec4ef',  
          'Reviewed' : '#cfffad',
          'Rejected' : '#42f498' ,
	  'IIQ Updated' : '#d7b5e5'
       };
       var rows = ctx.ListData.Row;
       for (var i=0;i< rows.length;i++)
       {
          var status = rows[i]["Status"];
          var rowId = GenerateIIDForListItem(ctx, rows[i]);
          var row = document.getElementById(rowId); 
          row.style.backgroundColor = statusColors[status];
       }
     }
   }); 
});
</script>

thanks in advance


#2

@Vikin I’ve fixed your code with code formatting. In the future, if you put your code between three backticks (or indent it all by 4 spaces), it will automatically get formatted :slight_smile:

```
code here
```

As for your problem, its similar to what you linked to, but probably for a different reason. The issue is that row is null. It’s null because rowId does not match the id of any elemen in your document so a valid element isn’t returned from getElementById, null is. Then trying to access null.style gives you the error.

You need to figure out why rowId is incorrect or why the element by that id doesn’t exist in the document when you expect it to.


#3

@senocular Thanks for your reply.
Backticks was not issue. I was not able to paste code, that’s why I have removed it.

I am not able to figure out why rowId is giving null value. Do you have alternate way for this.


#4

Problem has bee solved. Just added one line
if(!=null){row.style.backgroundColor = statusColors[status];}