2

I have small requiment, here I'm getting values to the table dynamically. Some times few fields returns some null/blank values. If it gives null values showing error message like "worderId[i].childNodes[0] is undefined" Please help me how to assign some default values to the null values.

if(worderId.length>0)
{

WOTableData= "<table cellpadding='0' cellspacing='1' border='0' width=100% class='display' id='WOData' ><thead><tr id='row1'><th>&nbsp;</th><th>worderId</th><th>wostatus</th></thead><tbody>";
var technologyImage="";
for(i=0;i<worderId.length;i++)
    {   

        if (!worderId[i].childNodes || !worderId[i].childNodes.length || !worderId[i].childNodes[0].nodeValue) {
                 worderId[i].childNodes =="---";
        }

        WOTableData=WOTableData+"<tr title='"+worderId[i].childNodes[0].nodeValue+"</td><td>"+ worderId[i].childNodes[0].nodeValue+"</td><td>"+wostatus[i].childNodes[0].nodeValue+"</td></tr>";                                            

    }
WOTableData=WOTableData+"</tbody></table>";


document.getElementById("WODataDiv").innerHTML = WOTableData;
}

1 Answer 1

1

I do not understand this piece of your code

 if (!worderId[i].childNodes || !worderId[i].childNodes.length || !worderId[i].childNodes[0].nodeValue) {
             worderId[i].childNodes =="---";
    }

    WOTableData=WOTableData+"<tr title='"+worderId[i].childNodes[0].nodeValue+"</td><td>"+ worderId[i].childNodes[0].nodeValue+"</td><td>"+wostatus[i].childNodes[0].nodeValue+"</td></tr>";

You are checking if worderId[i].childNodes does not exist or if it does and assuming it is an array, it has at least, one element or the first element of that array has a key named nodeValue. If either of these conditions are true, you assign

   worderId[i].childNodes =="---";

So now worderId[i].childNodes is a string. Then you access

  WOTableData=WOTableData+"<tr title='"+worderId[i].childNodes[0].nodeValue+"</td><td>"+ worderId[i].childNodes[0].nodeValue+"</td><td>"+wostatus[i].childNodes[0].nodeValue+"</td></tr>";

worderId[i].childNodes[0].nodeValue will give you an error in case any of the above conditions were true as in that case, you have assigned the value of worderId[i].childNodes as string and it is not an array.

So, to fix it you could do this

  var assignValue = "";
  if (!worderId[i].childNodes || !worderId[i].childNodes.length || !worderId[i].childNodes[0].nodeValue) {
            assignValue =  worderId[i].childNodes =="---";
    }else{
        assignValue = worderId[i].childNodes[0].nodeValue;
  }

    WOTableData=WOTableData+"<tr title='"+assignValue+"</td><td>"+ assignValue+"</td><td>"+assignValue+"</td></tr>";

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for explanations, I fixed my issue.
@Rajasekhar Glad it helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.