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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

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

Post 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
Last edited by myleow on Fri Nov 05, 2004 12:09 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post by myleow »

Thanks for your help, but I still get the same error after I changed to

Code: Select all

if(cb.checked)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

place an alerts in highlight function. Seems like cb is indeed undefined.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
Post Reply