Reputation: 31040
Say I have a div with innerHTML set using the HTML code for a rocket: 🚀
How do I do a conditional test for that character in javascript?
E.g. how do I get below to log "rocket"
let rocket = document.getElementById("rocket")
if (rocket.innerHTML == "🚀") console.log("rocket")
else console.log("no rocket");
<div id="rocket">
🚀
<div>
Upvotes: 0
Views: 134
Reputation: 61
I could achieve this way. Note that I've used ".innerText"
var ic = '🚀';
var r = document.getElementById("rocket");
if (r.innerText == ic) console.log("rocket");
else console.log("no rocket");
<div id="rocket">
🚀
</div>
Upvotes: 1
Reputation: 438
I tried using charCodeAt(0)
to get the unicode character code of the rocket and got 55357
as the result and the code below works.
let rocket = document.getElementById("rocket").innerText
if (rocket.charCodeAt(0) == 55357) console.log("rocket")
else console.log("no rocket");
<div id="rocket">
🚀
</div>
Hope this helps until a better answer comes. You can use console.log()
to check the character code if you want to have a different item instead of a rocket.
Upvotes: 1