html - Creating a new window that has a button in JavaScript -


hey im trying to learn javascript @ moment, , want able create button on page created in javascript, adds button index.html instead. please note running off of webstorm ide , don't have url/ dont know put window.open(____) because of that.

it creates new window saying "hello", there no button.

var mywindow=window.open(''); mywindow.document.write('hello'); var button=mywindow.document.createelement("newbutton"); button.onclick= function(){   alert("blabla"); }; var buttonparent= mywindow.document.getelementbyid("buttonparent"); buttonparent.appendchild(button) 

it looks though you're creating new window called mywindow, , writing text hello it. however, container id "buttonparent" not reside within new window, rather document index.html in.

try out:

var newdiv = document.createelement("div");       newdiv.id = "newbuttonparent" //make sure pick unique id       mywindow.document.appendchild(newdiv);     var buttonparent= mywindow.document.getelementbyid("newbuttonparent");      buttonparent.appendchild(button); 

edit: fixed typo. from:

var buttonparent= mywindow.document.getelementbyid("buttonparent");     

to

var buttonparent= mywindow.document.getelementbyid("newbuttonparent");  

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -