I'm trying to make a simple calculator and am stuck after trying to using the getAttribute method to retrieve my custom data-* attribute.
I made sure the var used to store the elements does indeed house them
<button type="button" class="btn btn-orange" data-num="*">*</button>
var btns = document.querySelectorAll(".btn");
for(var i = 0; i < btns.length; i++){
btns[i].addEventListener("click", function(){
var number = btns[i].getAttribute("data-num");
screen.value += number;
});
}
There are 15 buttons in total with class="btn". I have checked in the console and btns does indeed hold 15 elements. I don't understand why getAttribute("data-num")
is returning undefined when I click any of the buttons with the stated class.