Unable to update HTML using JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Unable to update HTML using JS

Hello, I am just trying to change HTML text from “offline” to “online”, but it does not work, can someone advise what did I do wrong?? Regards Ben https://code.sololearn.com/WWgOSttcb5rb/?ref=app

20th Oct 2019, 12:43 PM
Ben
4 Answers
+ 4
Try This: window.onload = () => { var status = document.getElementById("status") status.innerHTML = "online"; } Don't put the value of innerHTML in status as the current value is saved in the variable. Instead, put the element in the variable and change its property innerHTML. The error would still be there. This is because the JavaScript code is executed before the HTML. So, there is no p element when the code is executed. So, use : window.onload = function(){ ....code.... }
20th Oct 2019, 2:10 PM
Arnesh
Arnesh - avatar
+ 2
1st. your code run before the page load. either run the code at the bottom or adding onload listener should fix it https://code.sololearn.com/WRMTIixfc61T/?ref=app 2nd. when you try to get the value using innerHTML, it'll assign the variable by the value. you need it to pass as object reference. so remove innerHTML, then call from .innerHTML from the assigned variable when neededd. 3rd. you choice of variable, variable name status is already used in global scope by browser. and its string by default, its another strange thing about using var. somehow the value from getElementById converted into string, its maybe because the variable it replaced is a string. not sure. you can either using let instead of var, or change the variable name
20th Oct 2019, 2:14 PM
Taste
Taste - avatar
0
Rishi Anand and Taste , thank you so much!
20th Oct 2019, 2:21 PM
Ben
- 1
Use just need to use textContent document.getElementById("status").textContent = "online"
20th Oct 2019, 1:06 PM
Rishi Anand
Rishi Anand - avatar