Set checkbox status - works in IE but not FF

M

Martin

At the top of a page I'm displaying a check box thus:

<input type='checkbox' name='ckbx1' onClick='saveStatus("ckbx1");'>ABC

I then execute the following to set the check box to whatever it was
the last time it was changed:

<script type='text/javascript'>
document.getElementById('ckbx1').checked = getCookie('ckbx1');
</script>

This seems to work ok in IE6 but in Firefox a "...has no properties"
error is generated when the script executes. The same error is
generated when I click on the check box.

What I'm doing wrong ?
 
E

Erwin Moller

Martin said:
At the top of a page I'm displaying a check box thus:

<input type='checkbox' name='ckbx1' onClick='saveStatus("ckbx1");'>ABC

I then execute the following to set the check box to whatever it was
the last time it was changed:

<script type='text/javascript'>
document.getElementById('ckbx1').checked = getCookie('ckbx1');
</script>

This seems to work ok in IE6 but in Firefox a "...has no properties"
error is generated when the script executes. The same error is
generated when I click on the check box.

What I'm doing wrong ?


Hi,

What is wrong is simply that you use getElementById(), but didn't assign an
id to the checkbox.
You only named it. Names are for formelements (name/value-pairs) so the
receiving script can use them.
ids are for JS.

Solution: add the id to the box.

The reason IE does it 'right' is because IE does it wrong, and confuses name
with id.

Regards,
Erwin Moller
 
M

Martin

Hi,

What is wrong is simply that you use getElementById(), but didn't assign an
id to the checkbox.
You only named it. Names are for formelements (name/value-pairs) so the
receiving script can use them.
ids are for JS.

Solution: add the id to the box.

The reason IE does it 'right' is because IE does it wrong, and confuses name
with id.

Regards,
Erwin Moller

Thanks, Erwin - you're absolutely correct. It works now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top