开发者

catch null object in js

How can i catch the object is null or not an object. Actually i have wrote this line in if condition for solving it, but not working.

But Generating ERROR :- "object is null or not an object"

var preWynum = "";
function paint(Wynum)
{
    // alert("Wynum:"+Wynum +", preWynum:"+preWynum);
    if(document.getElementById(preWynum) && document.getElementById(Wynum开发者_运维知识库))
    {
      document.getElementById(preWynum).style.backgroundColor='white';
      document.getElementById(Wynum).style.backgroundColor='yellow';
    }

   preWynum = Wynum; 

}

I dont believe why it not running. Any other idea ?

preWynum and Wynum is the id of tr(table row).

I want to set background color with yellow to current selected row(that's id is Wynum).


The error is because you are passing an object (non-existent) to getElementById() when it expects a string:

// Put quotes around 'preWynum' and 'Wynum'
if(document.getElementById('preWynum') && document.getElementById('Wynum'))
{
  document.getElementById('preWynum').style.backgroundColor='white';
  document.getElementById('Wynum').style.backgroundColor='yellow';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜