1

How can I get a button to generate a random emoji (from a list of emojis) every time it is clicked using JavaScript.

3
  • 1
    Welcome. Please see How to ask and take the tour. You should add minimal reproducible code. Commented Sep 25, 2021 at 13:27
  • please show what have you tried so far Commented Sep 25, 2021 at 13:38
  • Please provide enough code so others can better understand or reproduce the problem. Commented Oct 3, 2021 at 12:44

2 Answers 2

2

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>`;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are replacing the emoji list in the funcition

var emojiList = ['&#128512', '&#128516', '&#128513', '&#128514'];
var display = document.getElementById('emojiDisplay');

function displayEmoji() {

 
  let randomEmojiIndex = Math.floor(Math.random() * emojiList.length);
  emoji = emojiList[randomEmojiIndex];
  
  display.innerHTML = `<h2>${emoji}</h2>`;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.