Set checkbox status - works in IE but not FF

Discussion in 'Javascript' started by Martin, May 16, 2007.

  1. Martin

    Martin Guest

    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 ?
     
    Martin, May 16, 2007
    #1
    1. Advertising

  2. Martin

    Erwin Moller Guest

    Martin wrote:

    > 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
     
    Erwin Moller, May 16, 2007
    #2
    1. Advertising

  3. Martin

    Martin Guest

    On Wed, 16 May 2007 18:13:38 +0200, Erwin Moller
    <> wrote:


    >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.
     
    Martin, May 16, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F. GEIGER
    Replies:
    3
    Views:
    783
    F. GEIGER
    Aug 6, 2004
  2. Michael Champagne
    Replies:
    4
    Views:
    342
    Matt Kruse
    Apr 20, 2004
  3. Randell D.
    Replies:
    5
    Views:
    150
  4. martialtiger

    Checkbox Validation Works on FF but not IE

    martialtiger, May 16, 2006, in forum: Javascript
    Replies:
    4
    Views:
    125
    Dr John Stockton
    May 23, 2006
  5. Replies:
    0
    Views:
    261
Loading...

Share This Page