+ 1

Why this simple js code doesn't work properly as expected

<!Doctype html> <html> <head> </head> <body> <p>nothing</p> <script> var x = document.getElementsByTagName("p"); x.innerHTML = "everything"; </script> </body> </html> //The output is: nothing :/

8th Nov 2017, 12:45 PM
Fahem
Fahem - avatar
1 ответ
+ 5
getElementsByTagName returns an array, you can't access innerHTML of an array. Use getElementsByTagName('p')[0]
8th Nov 2017, 12:47 PM
Muhammad Kamal
Muhammad Kamal - avatar