Message from JavaScript discussions

May 2019

— Besides, i didn't use id on the post, i used element tage

— 

So it would go like

<h2 class="foo">Bar</h2>
<h2 class="foo">Bar</h2>
<h2 class="foo">Bar</h2>
<script>
let elements=document.getElementsByClassName('foo')
var element_values = [ ]
elements.forEach(element =>{
element_values.push(element.innerHTML)
})
console.log(element_values)
</script>

— .

— You're opening and closing body tag before the markup

— I used forloop but it loop through only the first h2 element

— You're closing the body before your first h2 element

— Remove that closing body tag

— Sorry about that but say result

— Show the code.

— For the forloop?

— Yes

— <h2 class="foo">Bar</h2>
<h2 class="foo">Bar</h2>
<h2 class="foo">Bar</h2>
<script>
var elements=document.getElementsByClassName('foo')
for(var i=0;i<elements.length;i++){
console.log(elements[i])
}
</script>

Message permanent page