MarMan29 MarMan29 - 4 months ago 8
Javascript Question

Test for special & html code

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");

Answer

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>

Comments