javascript - How to place a TextNode into a div instead of body -


how place textnode div instead of body, thank in advance! sorry if unexperienced.

<!doctype html> <html>     <body>      <p>click button create h1 element text.</p>      <button onclick="myfunction()">try it</button>      <script>         function myfunction() {             var h = document.createelement("h1");             var t = document.createtextnode("hello");             h.appendchild(t);             document.body.appendchild(h);         }     </script>      </body> </html> 

one way of doing add div body , using getelementbyid

<body>  <div id="mybuttoncontainer">     <p>click button create h1 element text.</p>     <button onclick="myfunction()">try it</button> </div>  <div id="mydiv"></div>  <script>     function myfunction() {         var mybuttoncontainer = document.getelementbyid("mybuttoncontainer");         mybuttoncontainer.style.display='none';          var h = document.createelement("h1");         var t = document.createtextnode("hello");         h.appendchild(t);          var mydiv = document.getelementbyid("mydiv");         mydiv.appendchild(h);      } </script>  </body> 

you can check example here: http://jsfiddle.net/jmgwya58/1/


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -