Hi im new into java script and im trying to do a little project to train my skills, but i have a problem with getAttribute and im stuck :) maybe somebody can help me?
(function() {
var buttons = document.querySelectorAll('.btn');
buttons.forEach(function(button) {
button.addEventListener('click', function(e) {
var emotion = e.target.getAttribute("data-emo");
console.log(emotion);
})
});
})();
<div id="auswahl">
<button type="button" class="btn" data-emo="happy"> <img src="https://esquilo.io/png/thumb/ejzF8hfbnQpFxzA-Happy-Face-Emoji-PNG-File.png" width="100"> </button>
<button type="button" class="btn" data-emo="sad"> <img src="https://cdn.shopify.com/s/files/1/1061/1924/products/Very_sad_emoji_icon_png_large.png?v=1571606089" width="100"> </button>
</div>
The problem is that getAttribute returns null all the time, although data-emo exists. is there anything i forgot or did wrong?