How can I get a button to generate a random emoji (from a list of emojis) every time it is clicked using JavaScript.
-
1Welcome. Please see How to ask and take the tour. You should add minimal reproducible code.DecPK– DecPK2021-09-25 13:27:30 +00:00Commented Sep 25, 2021 at 13:27
-
please show what have you tried so farahsan– ahsan2021-09-25 13:38:05 +00:00Commented Sep 25, 2021 at 13:38
-
Please provide enough code so others can better understand or reproduce the problem.Community– Community Bot2021-10-03 12:44:41 +00:00Commented Oct 3, 2021 at 12:44
Add a comment
|
2 Answers
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>`;
}
Comments
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>`;
}