input checkbox onclick not working via DOM on IE7, FF, WebKit

Discussion in 'Javascript' started by Aaron Gray, Oct 31, 2008.

  1. Aaron Gray

    Aaron Gray Guest

    The following input checkbox onclick via DOM is not working on IE7, FF, or
    WebKit, but is working on Opera for some strange reason.

    http://www.aarongray.org/Test/JavaScript/checkbox.html

    Here's the code :-

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>

    <head>
    <script type="text/javascript" src="LibJS.js"></script>
    </head>

    <body onLoad="init()">

    <script>

    use( LibJS.HTML)

    function init()
    {
    test = document.createElement( "INPUT")
    test.type = "checkbox"
    test.checked = true
    test.onClick = "alert( 'test = ' + this.checked)"
    // test.setAttribute( "onClick", "alert( 'test = ' + this.checked)")

    document.body.appendChild( test)
    }

    </script>
    </body>
    </html>

    Hope I have not missed something too silly.

    Many thanks in advance,

    Aaron
     
    Aaron Gray, Oct 31, 2008
    #1
    1. Advertisements

  2. In case sensitive javascript the property name to which you should be
    assigning is 'onclick' (all lower case), and the value you should be
    assigning is a reference to a function object not a string primitive.

    Richard.
     
    Richard Cornford, Oct 31, 2008
    #2
    1. Advertisements

  3. Aaron Gray

    Aaron Gray Guest

    "function object not a string primitive" thats the one, silly, silly me !

    Have not done some JavaScript for a couple of weeks, amazing how quickly my
    brain chucks things out.

    Aaron
     
    Aaron Gray, Oct 31, 2008
    #3
  4. Aaron Gray

    SAM Guest

    Le 10/31/08 4:32 PM, Aaron Gray a écrit :
    test.onclick = function() { alert('test = ' + this.checked); };
    }
     
    SAM, Oct 31, 2008
    #4
  5. Aaron Gray

    Aaron Gray Guest

    Yes, thanks Sam, I now have everything working fine now :)

    Aaron
     
    Aaron Gray, Oct 31, 2008
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.