开发者

catch null object in js

开发者 https://www.devze.com 2023-03-14 11:54 出处:网络
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.

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

精彩评论

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