I create a lot of h1
whose class names are the same as below.
<h1 class="h1">One</h1>
<h1 class="h1">Two</h1>
<h1 class="h1">Three</h1>
<h1 class="h1">Four</h1>
Also create a button which will show values of these elements
<button id="button">Show</button>
And by clicking I want to show all the values of the elements with class "h1"
const button = document.getElementById('button');
var h1 = document.getElementsByClassName('h1');
button.addEventListener('click', function(){
for (var i = 0; i<=h1.lenght; i++){
alert(h1[i].value);
}
});
But nothing is showing.
` elements don't have a `value` attribute. Form elements do. Finally, since arrays are zero-indexed in JavaScript, you want `<`, not `<=`
– j08691 Jan 19 '18 at 18:24` does not have a `value`. It does have `innerHTML`. `value` is restricted to input-type elements.
– connexo Jan 19 '18 at 18:26` tag has no `value` attribute, we are atleast expected to see `undefined` in the alert. @connexo
– Sushanth -- Jan 19 '18 at 18:32