Skip to content Skip to sidebar Skip to footer

Uncaught TypeError: Cannot Read Property 'replace' Of Null Jqgrid

i am new to programming. when i try this below function, it works well unless there is a blank cell in the column. if there is any blank value in the cell then it is not working an

Solution 1:

You should test cellvalue != null before trying to parse the value. Testing for cellvalue != null means in JavaScript the same as cellvalue !== null || cellvalue !== undefined. In both cases you should don't use cellvalue.replace (or numval.replace).

The next possible problem in your code will be the usage of numeric values as input data. For example you can use 123 instead of "123". The Number type has no method replace and you could have one more error. I recommend you to use String(cellvalue) to convert the number to the string if it's not already the string.

Try something like

function growth (cellvalue) {
    if (cellvalue == null) { // test for null or undefined
        return "";
    }
    cellvalue = Number(String(cellvalue).replace("%",""));
    return '<span class="cellWithoutBackground" style="background-color:' +
        (cellvalue < 0 ? 'red' : 'green') +
        ';">' + cellvalue + '</span>';
}

Solution 2:

you can set globally condition like

function growth (cellvalue) { 

if(cellvalue != null || cellvalue!= '' || cellvalue != "") {
// do stuff here
 }
 else {
  //nothing do
}
}

Post a Comment for "Uncaught TypeError: Cannot Read Property 'replace' Of Null Jqgrid"