const allButtonsToStore = document.getElementsByClassName("button");
let reason = [];
for (let x=0; x < allButtonsToStore.length; x++) {
allButtonsToStore[x].addEventListener("click", function(){
reason[x] = this.textContent;
console.log("reason" + [x] + ": " + reason[x]);
reason[x] = this.value;
console.log("reason" + [x] + ": " + reason[x]);
reason[x] = this.data-spec;
console.log("reason" + [x] + ": " + reason[x]);
});
}
<div id="gamen" class="hidden">
<button class='button' data-next="gamen1" data-parent="gamen" data-spec="ram" value="2GB">Lichte games</button>
<button class='button' data-next="gamen1" data-parent="gamen" data-spec="videokaart" value="2GB">Middel games</button>
<button class='button' data-next="gamen1" data-parent="gamen" data-spec="processor" value="i9">Zware games</button>
</div>
With JS I'm getting all buttons and, for testing, I'm checking if I can get the attributes (I need the attributes and values later).
this.textContent; and reason[x] = this.value; are working fine. Only I can't seem to figure out how to get a custom attribute this.data-spec;
this.data-spec; gives ReferenceError: spec is not defined
this."data-spec"; and this."[data-spec]"; gives SyntaxError: Unexpected string
this.[data-spec]; and this.(data-spec); Gives an unexpected token error
this.getAttribute("data-spec");