innerhtml - How do I add a div to a page using javascript? -


so... want add following right before /body of document, can't seem find way make work:

document.body.innerhtml+="<div style=\"position:absolute; right:-10px; bottom:10px;\">response</div>\""); 

especially <body> element, shouldn't using innerhtml append elements element. easier way dom methods createelement, insertbefore or appendchild.

https://developer.mozilla.org/en-us/docs/dom/document.createelement

https://developer.mozilla.org/en-us/docs/dom/node.insertbefore

https://developer.mozilla.org/en-us/docs/dom/node.appendchild

try this:

var div = document.createelement("div"); div.style.position = "absolute"; div.style.right = "-10px"; div.style.bottom = "10px"; div.innerhtml = "response"; var lastchild = document.body.lastchild; document.body.insertbefore(div, lastchild.nextsibling); 

although guess make sense append body:

document.body.appendchild(div); 

(instead of last 2 lines in first example)

it depends on when you're calling code. of course work if executed in middle of <body>, want wait until body (dom) ready element appended @ real end of body. using like:

window.onload = function () {     // code above }; 

this make sure original <body> contents ready.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -