Want to create a button whose emoji changes with every mouse hover? Like on Discord? Then this article is for you!
![]()
Β
Step 1
The HTML code.
<div id="emoji">π</div>Nothing very complicated here: we declare a division with an id emoji.
Tip: Get your emojis from getemoji.com
Β
Step 2
Now let's style the id emoji.
#emoji {
font-size: 5rem;
filter: grayscale(1);
cursor: pointer;
transition: all 0.2s;
}
#emoji:hover {
transform: scale(1.3);
filter: grayscale(0);
}The grayscale() property allows you to transform an image into grayscale colors.
Β
Step 3
Let's move on to JavaScript by adding emojis.
const btn = document.querySelector("#emoji");
const emojis = [
"π", "π
", "π€£", "π",
"π", "π€", "π€¨", "π",
"π", "π", "π", "π",
"π€₯", "π΄", "π₯Ί", "π§"
];Β
Step 4
Let's detect the emoji hover.
btn.addEventListener("mouseover", () => {
btn.innerText = emojis[Math.floor(Math.random() * emojis.length)];
});As soon as we hover over the emoji, we replace the text of the id emoji with a new emoji.
To do this, we generate a random number and multiply it by the number of emojis we have.







