I don't really understand this code!!Please help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't really understand this code!!Please help me

window.onload = function() { var x = document.getElementById('demo'); x.onclick = function () { document.body.innerHTML = Date(); } }; My question is , what is "body" , and its functions ?

2nd Mar 2017, 3:09 PM
Thành Long
Thành Long - avatar
2 Answers
+ 4
<body> is the root html container of your document. It's necessarly present. 'document' is the document JS object, and it have a property 'body' providing a reference to <body> html element ( in the DOM -- Document Object Model ), as you can get by 'getElementById()' method. 'innerHTML' is a property shared by all html containers elements ( not <img>, <br>, <hr> and other 'empty' tag elements ), providing a read/write access to the Html source code content of it. So, if you have: <html> <head><title>demo</title></head> <body><p id="myP">almost empty page</p><body> </html> ... you'll get: alert(document.body.innerHTML); // output: <p id="myP">almost empty page</p> alert(document.getElementById("myP").innerHTML); // output: almost empty page ... and assigning it like: document.body.innerHTML+="<hr><p>content dynamically appended...</p>"; ... will produce: <body><p id="myP">almost empty page</p><hr><p>content dynamically appended...</p><body> ... in the source code, and applying changes on display ( new content is parsed by browser, as it was newly loaded ).
3rd Mar 2017, 9:08 AM
visph
visph - avatar
+ 1
body Is one part of an HTML document there you have most of the elements
2nd Mar 2017, 3:49 PM
sebipincha
sebipincha - avatar