0

If I have markup like this:

<div id="foo">&#xf067;</div>

and I want to detect later whether div#foo still contains that same character entity, I'd like to be able to do so by comparing it to &#xf067; rather than to (which in my code base is rather obtuse for maintenance purposes).

I've tried things like this (using jQuery):

console.log($('<textarea />').html($('#foo').html()).val());

But that seems to still output the nice little square "what you talkin' 'bout" character rather than the desired &#xf067;.

I'm open to plain JavaScript or jQuery-specific solutions.

jinglesthula
  • 4,446
  • 4
  • 45
  • 79
  • 1
    How about `console.log($('#foo').html())` ?? (I guess you may only get back entities for HTML-relevant metacharacters.) In any case you can form a JavaScript string with that character in it very easily: `"\uF067"` – Pointy Aug 20 '14 at 15:30
  • Doesn't look like you can get the raw data as you want it, [see here for info](http://stackoverflow.com/questions/15419209/getting-raw-text-content-of-html-element-with-html-uninterpreted) – musefan Aug 20 '14 at 15:35

1 Answers1

3

You can use a Unicode entity in JavaScript. For example:

(HTML: <div id='foo'>&#xf067;</div>)

JavaScript:

console.log($('#foo').html().charCodeAt(0).toString(16));
  //=> f067
console.log($('#foo').html().indexOf('\uf067'));
  //=> 0

Here's a JSFiddle.

tckmn
  • 57,719
  • 27
  • 114
  • 156