Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
In the below function i got error
Microsoft JScript runtime error: Unable to get value of the property "value": object is null or undefined;
Note: The below code is working fine in all IE browsers except IE9.



C#
for (loopIndex1 = 1; loopIndex1 < rowCount; loopIndex1++) {
            if (loopIndex1 < 8)
                saleplanTextIndex = "ctl00_Body_PlannerDetailsGV_ctl0" + parseInt(loopIndex1 + 2) + "_" + loopIndex1 + "salesplanTextID" + eleIndex;
            else
                saleplanTextIndex = "ctl00_Body_PlannerDetailsGV_ctl" + parseInt(loopIndex1 + 2) + "_" + loopIndex1 + "salesplanTextID" + eleIndex;

            //document.getElementById(saleplanTextIndex).value
            if (document.getElementById(saleplanTextIndex).value == "") {
                document.getElementById(saleplanTextIndex).value = 0;
            }
            colSum = colSum + parseInt(document.getElementById(saleplanTextIndex).value);
        }
Posted

The string argument in document.getElementById must be in quotation marks.

The exception is not revealed by JavaScript immediately due to implicit nature of variable declarations. The case is kind of funny. First, the name like saleplanTextIndex (without quotation marks) is handled as a non-initialized variable, valid as such. Than, naturally document.getElementById returns undefined object (which always takes place when the HTML DOM element is simply not found, even if your argument was a valid string, but the id is not found), and only then the attempt to dereference the undefined or null object by using its (non-existent) property value throws the exception.

After you do this fix, you will still have the same problem if id of the element is not found, so make sure the elements you are looking for are properly and uniquely identified with their id attribute values.

—SA
 
Share this answer
 
Here is my complete function:

function SummationSalesPlan(rowIDs) {
var loopIndex1 = 0;
var uspreplySum = 0;
var salesplanSum = 0;
var agentSum = 0;
var eleName = "";
var rowCount = rowIDs.split('|')[rowIDs.split('|').length - 1];
var eleIndex = rowIDs.split('|')[rowIDs.split('|').length - 2]; //Gives the current column
var restSum = rowIDs.split('|')[rowIDs.split('|').length - 3];
var colSum = 0;
var saleplanTextIndex = "";
document.getElementById("ctl00_Body_IsTermUpdated").value = 1;

for (loopIndex1 = 1; loopIndex1 < rowCount; loopIndex1++) {
if (loopIndex1 < 8)
saleplanTextIndex = "ctl00_Body_PlannerDetailsGV_ctl0" + parseInt(loopIndex1 + 2) + "_" + loopIndex1 + "salesplanTextID" + eleIndex;
else
saleplanTextIndex = "ctl00_Body_PlannerDetailsGV_ctl" + parseInt(loopIndex1 + 2) + "_" + loopIndex1 + "salesplanTextID" + eleIndex;

//document.getElementById(saleplanTextIndex).value
if (document.getElementById(saleplanTextIndex).value == "") {
document.getElementById(saleplanTextIndex).value = 0;
}
colSum = colSum + parseInt(document.getElementById(saleplanTextIndex).value);
}
//ctl00_Body_PlannerGV_ctl04_2textID14
//alert("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1));

uspreplySum = document.getElementById("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1)).innerText; //USP Reply
document.getElementById("ctl00_Body_PlannerGV_ctl05_3lblID" + eval(eleIndex - 1)).innerText = colSum; //Sales Plan
salesplanSum = colSum;

if (isNaN(parseInt(uspreplySum)) == true)
uspreplySum = 0;
if (isNaN(parseInt(salesplanSum)) == true)
salesplanSum = 0;
if (isNaN(parseInt(document.getElementById("ctl00_Body_PlannerGV_ctl06_4lblID" + eval(eleIndex - 1)).innerText)) == true)
document.getElementById("ctl00_Body_PlannerGV_ctl06_4lblID" + eval(eleIndex - 1)).innerText = 0;//Diff Value

if (document.getElementById("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1)).disabled == true) {
//document.getElementById("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1)).value = parseInt(document.getElementById("ctl00_Body_PlannerGV_ctl06_4lblID" + eval(eleIndex - 1)).innerText) + parseInt(salesplanSum); //Diff
document.getElementById("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1)).value = salesplanSum;
}

document.getElementById("ctl00_Body_PlannerGV_ctl06_4lblID" + eval(eleIndex - 1)).innerText = parseInt(document.getElementById("ctl00_Body_PlannerGV_ctl04_2textID" + eval(eleIndex - 1)).value) - parseInt(salesplanSum);

// else
// document.getElementById("ctl00_Body_PlannerGV_ctl06_4lblID" + eval(eleIndex - 1)).innerText = parseInt(salesplanSum);

//Calculate T-Terms for particular agent in six months.
for (loopIndex1 = 1; loopIndex1 < rowIDs.split('|').length - 3; loopIndex1++) {
if (isNaN(parseInt(document.getElementById(rowIDs.split('|')[loopIndex1]).value)) == true)
document.getElementById(rowIDs.split('|')[loopIndex1]).value = 0;

agentSum = parseInt(agentSum) + parseInt(document.getElementById(rowIDs.split('|')[loopIndex1]).value);
}

agentSum = parseInt(restSum) + parseInt(agentSum);
document.getElementById(rowIDs.split('|')[0]).innerText = agentSum;

SumSalesTerm();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900