Why wont this work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why wont this work?

Why wont this code change the h1 tag, it just stays at its default text entered in the html? var x = 1; var y = 1; var fib = document.getElementsByTagName("h1"); function increase() { x += y; y = x-y; fib.innerHTML = y ; } setInterval(increase, 1000);

10th Jun 2017, 9:42 AM
Elias
Elias - avatar
2 Answers
+ 3
When you call document.getElementsByTagName("h1"), it returns an array of h1 tags; even if you have just one h1 tag. You must use indexes, e.g. fib[0].innerHTML.
10th Jun 2017, 9:52 AM
$machitgarha
$machitgarha - avatar
+ 2
Oh, youre right, thank you @Mohammad Amin Chitgarha
10th Jun 2017, 9:57 AM
Elias
Elias - avatar