+ 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
4 ответов
+ 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....
}
+ 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
0
Rishi Anand and Taste , thank you so much!
- 1
Use just need to use textContent
document.getElementById("status").textContent = "online"