I declared a variable called playerMoves. I am attempting to store the clicks in the playerMoves array like this :
function storeClicks(){
$('.button').each(function(elem) {
$(this).click(function(e) {
playerMoves.push($(this).val());
console.log(playerMoves);
// Player has made their moves, validate their moves
//validate(playerMoves);
});
});
}
When I open the Firefox dev tools and go to the console it outputs something this :
Each time one of the divs that represent a Simon Says button is clicked it adds to the array like it should, but instead of displaying the HTML data (i.e. a 1 for the green button, 2 for the red button, etc) it just shows an empty string.
That’s true in the sense that nonstandard attributes aren’t valid according to the HTML spec, but nonstandard attributes can still be accessed with JS:
To be honest, I didn’t know about its existence either until a couple of Google searches ago!
The curious thing is, like many jQuery methods, it can be used as either a getter or a setter, but when used as a setter, it doesn’t actually add the corresponding data-* attribute to the element (but it does allow you to access it through the corresponding getter method).
$('#myDiv').data('newData', 'newVal');
// nothing happens to `#myDiv` in the DOM, but...
$('#myDiv').data();
// now returns {properDataAttribute: "myVal", newData: "newVal"}
Now that’s what I call a textbook example of vendor lock-in!