Page 1 of 1

[SOLVED]Help on JavaScript "is null or not an object&qu

Posted: Thu Nov 04, 2004 10:59 pm
by myleow
I am getting this error on Internet Explorer 6.0

Code: Select all

Char: 3
Error: 'cb.checked' is null or not an object
Code: 0
Although I.E. is reporting a Javascript error, it works fine in highlighting the row that i just clicked on the checkbox. There are no Error warning when using Netscape or Firefox. Anyone know why?

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
function getElement(id)
&#123;
  if(document.all)
    return document.all&#1111;id];
  else
    return document.getElementById(id);
&#125;
function highlight(num)
&#123;
  cb = getElement('cb'+num);
  row_cb = getElement('row'+num);
  if(cb.checked == true)
    row_cb.style.backgroundColor = "lightblue";
  else
    row_cb.style.backgroundColor = "white";
&#125;
</SCRIPT>


<tr id="row0"><td><input id="cb0" type="checkbox" name="deletelist&#1111;0]" value=66 onClick="highlight('0')"></input></td></tr>


<tr id="row1"><td><input id="cb1" type="checkbox" name="deletelist&#1111;1]" value=64 onClick="highlight('1')"></input></td></tr>
Thank you in advance.

Regards
Mian

Posted: Thu Nov 04, 2004 11:18 pm
by Weirdan
It does work on IE 6.0.2900.2180 sp2. I would suggest to change:

Code: Select all

//.......
if(cb.checked == true) 
//......
to

Code: Select all

//......
if(cb.checked) 
//.......
though

Posted: Thu Nov 04, 2004 11:25 pm
by myleow
Thanks for your help, but I still get the same error after I changed to

Code: Select all

if(cb.checked)

Posted: Fri Nov 05, 2004 12:52 am
by Weirdan
place an alerts in highlight function. Seems like cb is indeed undefined.

Posted: Fri Nov 05, 2004 2:54 am
by phpScott
if sometime find that IE doesn't like to do the getElmentById() like you have done it, so i usually do.

Code: Select all

function highlight(num)
&#123;
  tCb='cb'+num  ;
  cb = getElement(tCb);
  tRow='row'+nim;
  row_cb = getElement(tRow);
  if(cb.checked == true)
    row_cb.style.backgroundColor = "lightblue";
  else
    row_cb.style.backgroundColor = "white";
&#125;
The other big difference that I find when coding JS across the browsers is that in mozilla based browsers using a variable name the same as an element name or id is fine but under IE it doens't work.