2

I set a span using:

<span id=\"xxx\">&#9656;</span>

Can someone tell me why the following test doesn't work:

if ($("#xxx").text() == "&#9656;")
         alert("hello");
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
MarMan29
  • 719
  • 9
  • 22
  • This might also be informative: [Getting raw text content of HTML element with HTML uninterpreted](http://stackoverflow.com/questions/15419209/getting-raw-text-content-of-html-element-with-html-uninterpreted) – showdev Jul 28 '16 at 18:24

4 Answers4

3

because &#9656; 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");
Pedro Estrada
  • 2,384
  • 2
  • 16
  • 24
3

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 &#9656;, 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+";" === "&#9656;")
  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>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

"&#9656‌;" 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">&#9656;</span>
Doc
  • 281
  • 3
  • 5
-1

Or you can do this:

<span id=\"xxx\">&amp;#9656;</span>

if ($("#xxx").text() == "&#9656;")
     alert("hello");
jonju
  • 2,711
  • 1
  • 13
  • 19
  • The browser will not render the special character this way – empiric Jul 28 '16 at 18:42
  • I thought the OP wanted to check "▸" not the literal character. – jonju Jul 28 '16 at 18:49
  • yes this is correct, but the ▸ icon will not be displayed on the frontend anymore. It will literally display `▸` on the frontend – empiric Jul 28 '16 at 20:36
  • I saw OP's question "how do you test for ▸ ". in one of the comments above,this led me post my answer and besides I do not have enough reputation to post comment so I can confirm with him – jonju Jul 28 '16 at 20:42