Guest User

Dynamically Adding HTML (with Javascript) to a DIV using INNERHTML - Works on Internet Explorer

a guest
Oct 1st, 2010
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.25 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <script>
  4.             /* the input type = hidden is necessary or the JS won't be accessible */
  5.             /* script defer tag is also necessary */
  6.             function insertHTMLOnButtonPress() {
  7.                 var s = "<html><input type='hidden' id='dummyHidden'/><head><script defer='defer'>function dynamicallyInsertedFunction() { alert('Successfully called - dynamicallyInsertedFunction'); } </sc" + "ript></head><body><input type='text' value='Hello World'/><input type='button' onClick='dynamicallyInsertedFunction();' value='Call Dynamically Inserted Method'/></body></html>";
  8.                 /* Clearing out innerHTML is also required to flush the innerHTML so that repeated attempts - i.e new HTML/JS should work */
  9.                 document.getElementById('tempBody').innerHTML = '';
  10.                 document.getElementById('tempBody').innerHTML = s;
  11.             }
  12.         </script>
  13.     </head>
  14.     <body>
  15.         **********************************<br/>
  16.         <!-- Don't close the div inline, causes some problem and replaces the buttons too -->
  17.         <div id="tempBody"></div>
  18.         **********************************<br/>
  19.         <input type="button" onClick="insertHTMLOnButtonPress();" value="Insert some dynamic html"/>
  20.         <input type="button" onClick="dynamicallyInsertedFunction();" value="Call Dynamically Inserted Method"/>
  21.     </body>
  22. </html>
Advertisement
Add Comment
Please, Sign In to add comment