I'm new to javascript. I'm trying to use an alert that shows button data when clicking a specific button.
But here it's just alerting same button name always. How to fix that?
function getElement(k) {
var elm = document.getElementById(k);
/*alert ("hai");*/
return elm;
}
for (var i = 1; i <= 6; i++) {
getElement("button" + i).addEventListener("click", function() {
alert("clicked on button" + i);
});
}
li {
list-style: none;
}
<li>
<div class="button-holder"><button id="button1" class="button">button1</button>
</div>
</li>
<li>
<div class="button-holder">
<button id="button2" class="button">button2</button>
</div>
</li>
<li>
<div class="button-holder">
<button id="button3" class="button">button3</button>
</div>
</li>
<li>
<div class="button-holder">
<button id="button4" class="button">button4</button>
</div>
</li>