I want to add a heart shaped icon as a button. On a click of it should toggle filled/non-filled. I got two - ♡ and ❤ But when I am comparing it as a text. I cant. Because it appears as an image. Here is my try:
function setMyFavorite(){
alert('SetMyFavorite for '+ $("#heart").html());
if($("#heart").html()=="♡"){
$("#heart").html("❤");
}
if($("#heart").html()=="❤"){
$("#heart").html("♡");
}
}
Here is my HTML code
<div id="heart" title="Set as Favorite" onclick="javascript:setMyFavorite();">♡</div>
How Can I compare it?
You can compare using the unicode characters.
const whiteHeart = '\u2661';
const blackHeart = '\u2665';
const button = document.querySelector('button');
button.addEventListener('click', toggle);
function toggle() {
const like = button.textContent;
if(like==whiteHeart) {
button.textContent = blackHeart;
} else {
button.textContent = whiteHeart;
}
}
<button>♡</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With