Code Playground Issue. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Code Playground Issue.

why this code not working on Sololearn codeplayground? // suppose we have two 'h1' tags with content of 'hello' in html var test = document.getElementsByTagName('h1'); console.log(Array.from(test).forEach(item => { console.log(item); // null return item.value; // output undefined })) // and var test = document.querySelectorAll('h1') console.log(test.forEach(item => { return item.value; // output undefined })) // why null? does the codeplayground do not support these futures? thanks.

7th Feb 2018, 1:52 AM
Habel Justin
Habel Justin - avatar
6 Answers
+ 6
Did you wait for the body to load? Otherwise, the list is empty.
7th Feb 2018, 2:20 AM
John Wells
John Wells - avatar
+ 1
I don’t remember much about JS, but one problem is that getElementsByTagName does not get the class or id, it gets the tag name. That is html, head, p, body, any of those.
7th Feb 2018, 1:56 AM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
@jayden it's not. console.log() is valid
7th Feb 2018, 2:14 AM
Habel Justin
Habel Justin - avatar
0
opss i means h1 over there
7th Feb 2018, 1:57 AM
Habel Justin
Habel Justin - avatar
0
@john yes already sir. document.addEventListener('DOMContentLoaded', () => { console.log('ready') // output 'ready' test = document.getElementsByTagName('h1') console.log(test) // output [object HTMLCollection] console.log(Array.from(test).forEach(item => { return item.value; // output undefined })) })
7th Feb 2018, 2:30 AM
Habel Justin
Habel Justin - avatar
0
Nevermind guys, i think i just solved the issue. trying to consoling the results for changing the element content does not actually get consoled, because i do not return anything to the console that trying to output. I'm so lol 😂 document.addEventListener('DOMContentLoaded', () => { console.log('ready') test = document.getElementsByTagName('h1') console.log(test) // output [object HTMLCollection] console.log(Array.from(test).forEach(item => { item.innerHTML = 'NEW'; // change the content to "NEW" })) })
7th Feb 2018, 2:43 AM
Habel Justin
Habel Justin - avatar