In this code example, we create a smiley (you can use an emoji as well) that when clicked turns from happy to sad and vice-versa.
HTML
<div class="smiley happy" onclick="toggleSmiley(this)"></div>
CSS
.smiley {
width: 100px;
height: 100px;
cursor: pointer;
transition: transform 2.4s ease;
font-size: 100px;
display: flex;
align-items: center;
justify-content: center;
margin: auto;
position: relative;
}
.happy::before {
content: '\1F600';
}
.sad::before {
content: '\1F622';
}
JavaScript
function toggleSmiley(element) {
element.classList.toggle('happy');
element.classList.toggle('sad');
}
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!