SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkbox.onclick not working in IE

    I'm trying to create a checkbox with Javascript, add it to the DOM, and register an onclick handler for it. My test program works in Firefox2, but not in IE6. Here's a link to the test, and the page code is below. Please let me know if you see what I'm doing wrong. thanks in advance!

    http://www.defy.org/dhtml/checkbox.html

    Code:
    <html>
      <head>
    
        <title>Checkbox test.</title>
    
        <script type="text/javascript">
          function doSomething() {
              alert("do something");
          }
        </script>
    
      </head>
    
      <body>
    
      
      <div id="myDiv"></div>
      
      <script type="text/javascript">
    
         var stdCheckBox = document.createElement('input');
         stdCheckBox.setAttribute('name', 'theName');
         stdCheckBox.setAttribute('type', 'checkbox');
         stdCheckBox.setAttribute('id', 'theCheckbox');
         var x=document.getElementById("myDiv");
         x.appendChild(stdCheckBox);
    
         // try two different ways to register an onclick event handler.
         stdCheckBox.setAttribute('onclick', 'doSomething()');
         stdCheckBox.onclick='doSomething()';
    
      </script>
    
      </body>
    </html>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi nanreh, welcome to SPF!

    Try this:

    stdCheckBox.onclick = doSomething;

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    It worked...

    thanks for the quick response!
    n


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •