I set a span using:
<span id=\"xxx\">▸</span>
Can someone tell me why the following test doesn't work:
if ($("#xxx").text() == "▸")
alert("hello");
I set a span using:
<span id=\"xxx\">▸</span>
Can someone tell me why the following test doesn't work:
if ($("#xxx").text() == "▸")
alert("hello");
because ▸
is actually this character ▸
When you do $('#xxx').text()
you'll get ▸
Try doing it this way:
if ($("#xxx").text() == "▸")
alert("hello");
From looking at the other questions linked in this one. Looks like the way to do what you're looking for is:
if ($("#xxx").text().charCodeAt() == '9656')
alert("hello");
If you don't want to use the other answer that make a direct symbol condition $("#xxx").text() == "▸"
and you want to check the span text ▸
, you could use .charCodeAt(0)
to get the special symbole number then add &#
and ;
in your condition :
var special_symbol_number = $("#xxx").text().charCodeAt(0);
if ( "&#"+special_symbol_number+";" === "▸")
alert("hello");
Or you could check just with special symbol number as :
if ($("#xxx").text().charCodeAt(0) === '9656')
alert("hello");
Hopet this helps.
if ($("#xxx").text().charCodeAt(0) === '9656')
alert("hello");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="xxx">9656</span>
"▸" is a that applies only to HTML and not to the Javascript. In HTML its' called an Entity, and it gets replaced with the literal character (▸). But it doesn't do this in JavaScript.
To check for the character in JavaScript, just put character in literally:
if ($("#xxx").text() == "▸")
alert("hello");
You can get the character by copying it in your browser (select the ▸, and hit cmd-c or ctrl-c), and then pasting it in the JavaScript file.
The reason HTML uses entities is because it has some characters (like < and >) that can be parsed as actual parts of the code (try writing <text> in HTML; it won't be visible because HTML thinks you want an element); the entities allow these values to be parsed literally even when they look like code. Here is a working snippet, where the JavaScript successfully checks the contents of the element:
if ($("#xxx").text() == "▸")
alert("hello");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="xxx">▸</span>
Or you can do this:
<span id=\"xxx\">&#9656;</span>
if ($("#xxx").text() == "▸")
alert("hello");