Connecting Tech Pros Worldwide Help | Site Map

Set checkbox status - works in IE but not FF

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 16th, 2007, 03:55 PM
Martin
Guest
 
Posts: n/a
Default Set checkbox status - works in IE but not FF

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 ?

  #2  
Old May 16th, 2007, 04:15 PM
Erwin Moller
Guest
 
Posts: n/a
Default Re: Set checkbox status - works in IE but not FF

Martin wrote:
Quote:
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
  #3  
Old May 16th, 2007, 07:15 PM
Martin
Guest
 
Posts: n/a
Default Re: Set checkbox status - works in IE but not FF

On Wed, 16 May 2007 18:13:38 +0200, Erwin Moller
<since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrote:

Quote:
>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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.