0

Help for clear this code

I can't understand what was the mistake function hi() { var ab=document.getElementById('start'); ab.innerHTML="hello"; } hi();

4th Feb 2019, 7:35 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar
11 Respostas
+ 2
You have to load it after HTML, either by,script tags at the bottom of body. Or by using window.onload = () => {your code}.
4th Feb 2019, 7:55 PM
Maneren
Maneren - avatar
4th Feb 2019, 8:08 PM
Maneren
Maneren - avatar
+ 2
It loades in head tag, so it executes before there is any element. Then you have to use window.onload
4th Feb 2019, 8:11 PM
Maneren
Maneren - avatar
+ 2
If placed in JS page it is automatically placed to head tag in HTML. So it executes when head is loaded. But in that time there is no body tag (with all child nodes) loaded. So it throws undefined as the variable value. Got it?
4th Feb 2019, 8:16 PM
Maneren
Maneren - avatar
+ 1
I used script tag bottom of the body but error is show innerHTML null
4th Feb 2019, 8:02 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar
+ 1
Are you sure you have element with id 'start'?
4th Feb 2019, 8:03 PM
Maneren
Maneren - avatar
+ 1
Or better post whole code via code playground, then i will be able to help you more
4th Feb 2019, 8:04 PM
Maneren
Maneren - avatar
4th Feb 2019, 8:06 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar
+ 1
But if I write this code in js section what is the wrong?
4th Feb 2019, 8:10 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar
+ 1
Sorry but plz explain more
4th Feb 2019, 8:14 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar
+ 1
BTW thank you very much for helping
4th Feb 2019, 8:16 PM
SOUVIK RAY BARMAN
SOUVIK RAY BARMAN - avatar