Search code examples
jqueryhtmlentitysymbols

Get HTML entity of character symbol in jQuery


If a square root symbol exists in a I like to color it green. So I have something like this:

<div class=”cell”>foo</div>
<div class=”cell”>bar</div>
<div class=”cell”>&radic;</div>
<div class=”cell”>foo</div>
<div class=”cell”>&radic;</div>

My attempt with jQuery:

$(".value_spec_table_description_div_cs").each(function(){

    if ($(this).html() === "&radic;"){
        $(this).css("color" , "rgba(0,128,0,1.0)");
        }
    });

Unfortunately it seems html() is not converting the symbol √ into HTML entity again so the statement never returns true.

I tried a lot like:

var test = $("#table").html();

var html = $("<textarea/>").html(test).html();

alert(html);

Every special character is in entity unless the root square symbol into &radic;. How can I solve this?


Solution

  • Browsers normalise HTML when they convert it to a DOM. Short of making an Ajax request to get the source code of the page, writing a custom parser, finding the HTML you care about, and comparing that you can't really do this.

    Better to just compare text:

    if ($(this).text() === "√"){