user16956594
user16956594

Reputation:

Generate random using button using JavaScript

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

Upvotes: 1

Views: 533

Answers (2)

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>`;

}

Upvotes: 0

loops
loops

Reputation: 5635

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>`;
}

Upvotes: 2

Related Questions