Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOM buttons and onclick events

    Hello,

    I'm trying to do something fairly straightforward: write a GreaseMonkey script that uses DOM to add a button to a page. Here's my code:

    Code:
    var elmReportButton = document.createElement('button');
    elmReportButton.appendChild(document.createTextNode("Report"));
    elmReportButton.onclick = function(){ alert("Hi!"); }
    elmThreadURLButton.appendChild(elmReportButton);
    Eventually I want to replace alert("Hi!"); with something more interesting, but let's use that as an example for now.

    Unfortunately, this doesn't work. The JavaScript console just says "Component not defined" on the onclick line. (I've tried this with both function references and anonymous function declarations, and get the same result each time). What am I doing wrong?

    Thanks!

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try creating an input type="button" instead
    Code:
    var elmReportButton = document.createElement('input');
    elmReportButton.setAttribute('type','button');
    elmReportButton.setAttribute('value','Report');
    elmReportButton.onclick = function(){ alert("Hi!"); }
    elmThreadURLButton.appendChild(elmReportButton);
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope. Still get the same error.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try adding the event to the element after you've added it to the page's DOM.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the suggestion, but that didn't work either.

    Does it matter that this is a GreaseMonkey script, and that the DOM elements are being added to the page after it's already been created?

    Does it matter that I'm not using a <form> element?

    I'm racking my brains on this one but just can't seem to understand why the script won't execute.

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    dont know about GreaseMonkey script
    but as expected this works

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function Add(){
    var elmReportButton = document.createElement('input');
    elmReportButton.setAttribute('type','button');
    elmReportButton.setAttribute('value','Report');
    elmReportButton.onclick = function(){ alert("Hi!"); }
    document.getElementById('fred').appendChild(elmReportButton);
    }
    /*]]>*/
    </script></head>
    
    <body onload="Add();">
    <div id="fred"></div>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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