How can I get a button to generate a random emoji (from a list of emojis) every time it is clicked using JavaScript.
CodePudding user response:
You need to store your emojis as their hex values, not as their HTML entity encoded form. Also, it seems the Unicode values you picked map to Japanese characters, not emojis. Here's a working (except for actually using Japanese characters) script that I cleaned up a bit:
var emojis = [0x128512, 0x128516, 0x128513, 0x128514];
var display = document.getElementById('emojiDisplay');
function displayEmoji(){
var random = Math.floor(Math.random() * emojis.length);
var emoji = emojis[random];
display.innerHTML=`<h2>${String.fromCharCode(emoji)}</h2>`;
}
CodePudding user response:
You are replacing the emoji list in the funcition
var emojiList = ['😀', '😄', '😁', '😂'];
var display = document.getElementById('emojiDisplay');
function displayEmoji() {
let randomEmojiIndex = Math.floor(Math.random() * emojiList.length);
emoji = emojiList[randomEmojiIndex];
display.innerHTML = `<h2>${emoji}</h2>`;
}