1

So I've got a JSON file (pulled in from an API) where I'm doing some comparisons. I fetch the content of the JSON file and put it on some buttons (as button text). I then do a comparison test for equality between the button text and text in the JSON file.

This works in 99% of my use cases however:

Some words in my JSON file have accents on them (either é or so on). When I try and do a comparison in jQuery, even though I'm using the:

   $(this).html()

for my button text, the equality test fails. The word in the JSON File might read:

   Frédéric Auguste Bartholdi 

but on my button it reads:

    Frédéric Auguste Bartholdi

I thought if I did a comparison test such as:

    if($(this).html() === jsonFile[0])){...}

that would do the trick, but it doesn't seem to do it. For clarity (this) refers to my button and I can't really do much about the JSON file - can't be changed.

Does anyone have any ideas?

xn139
  • 375
  • 1
  • 4
  • 16
  • Refer to [this answer](http://stackoverflow.com/a/18160397/2019247). You need to replace the accented characters with their english equivalents before comparing. – 31piy Feb 24 '17 at 05:30
  • maybe this? http://stackoverflow.com/questions/3330345/javascript-jquery-convert-special-html-characters – A. L Feb 24 '17 at 05:31

3 Answers3

2

It easy:

function html_entities(str){
    return $('<textarea />').html(str).text();
}
$('div').text(html_entities("Fr&eacute;d&eacute;ric Auguste Bartholdi"));

See at: https://jsfiddle.net/kilotonna/yy46na0g/

Thanks for: HTML Entity Decode

Community
  • 1
  • 1
Choo Hwan
  • 416
  • 3
  • 12
0

Instead of using $(this).html() use $(this).text(). Because .html() - will returns the html format of those unicodes.

web developer
  • 457
  • 2
  • 10
  • 25
0

you should try $(this).text() instead of $(this).html()

Sudhanshu Jain
  • 494
  • 3
  • 11