+ 1
How to display user name on the output by using js
13 Answers
+ 2
He use window.onload, because on sololearn js code is same as we type it in head on html, so we can't select any elements, because browser first read head and then body, so elements in body don't exist jet.
You can place js code in script tag right before </body>, and your js code inside this script tag.
But if you wanna use js tab on sololearn using window.onload is solution, it will wait for document to load (browser to read full html page), and then run js code.
About using index [0],you can see he select element by class name,to don't change your html code. If you select element by class name, you always get array of node elements. To access some of them you use index, because this is first element in list, you use index 0.
Even if you have 1 element with this class name it will always return array.
+ 1
A͢J
Thanks bro
But I have two doubts
Why to use window onload?
Why u have mentioned [0]?
+ 1
PanicS
If we write js code in script tag it is no need to use window.onload
Am I right?
+ 1
Yes, but only if this script is before </body> and elements are above this script tag, so it load at end of document, if you place it in head or in body but elements are bellow script, you will get error.
You can test this, by editing code AJ posted, to see error message, so if ever have this error you will know why you get this and how to solve.
+ 1
PanicS
Thankyou bro
+ 1
What is the difference between both console .log and document.write SSTech SS
+ 1
If you use console.log() it will show the output on console but if you use document.write() it show the output on the browser.
+ 1
Manel Nella
html, css, java, c++
0
Use prompt to take input and document.write to display output
0
A͢J
Here In this code I want to display user name in the box how can I display it?
https://code.sololearn.com/W1Sn3RkTD4Us/?ref=app
0
Your answer is; console.log("name")
0
Or use, document.write("name")