+ 1

How to display user name on the output by using js

26th Mar 2022, 8:32 AM
UNKNOWN
13 odpowiedzi
+ 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.
26th Mar 2022, 9:20 AM
PanicS
PanicS - avatar
+ 1
A͢J Thanks bro But I have two doubts Why to use window onload? Why u have mentioned [0]?
26th Mar 2022, 9:09 AM
UNKNOWN
+ 1
PanicS If we write js code in script tag it is no need to use window.onload Am I right?
26th Mar 2022, 9:26 AM
UNKNOWN
+ 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.
26th Mar 2022, 9:45 AM
PanicS
PanicS - avatar
+ 1
PanicS Thankyou bro
26th Mar 2022, 9:46 AM
UNKNOWN
+ 1
What is the difference between both console .log and document.write SSTech SS
26th Mar 2022, 12:57 PM
UNKNOWN
+ 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.
26th Mar 2022, 1:00 PM
SSTech SS
+ 1
Manel Nella html, css, java, c++
27th Mar 2022, 7:03 PM
imad Bouhroud
imad Bouhroud - avatar
0
Use prompt to take input and document.write to display output
26th Mar 2022, 8:47 AM
A͢J
A͢J - avatar
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
26th Mar 2022, 8:50 AM
UNKNOWN
0
Your answer is; console.log("name")
26th Mar 2022, 12:55 PM
SSTech SS
0
Or use, document.write("name")
26th Mar 2022, 12:56 PM
SSTech SS