1

I have a simple script that is looking for a heading to perform a function when the heading matched. I am running into issues with special characters like copyright and registered trademarks.

var isDisclosure = $('.disclosure-container h3').html();

if ( isDisclosure == "Awesome Product<sup>&#174;</sup> Disclosures" ) {
        alert('zomg');
    }

The html looks like this in firebug:

<h3 class="inline">
    Awesome Product
    <sup>®</sup>
    Disclosures
</h3>

Upon viewing the source the html looks like this...

<h3 class="inline">Awesome Product<sup>&#174;</sup> Disclosures</h3>

So when I add that html to the if statement, I get an added extra character and it doesn't work... like so...

if ( isDisclosure == "Awesome Product<sup>©</sup> Disclosures" )

I am very open to just searching for "Awesome Product" with a wildcard or something at the end.

TurboSupra
  • 119
  • 1
  • 10
  • Well, in that case you can do `if(isDisclosure.indexOf("Awesome Product") === 0) { ...` :) – techfoobar Jul 17 '13 at 15:38
  • Have you tried using a function which converts HTML entities like the one [here](http://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript) and using that to fill your string. In other words `if ( isDisclosure == "Awesome Product"+htmlDecode("®")+" Disclosures" )` – Jnatalzia Jul 17 '13 at 15:40

1 Answers1

1

You could use a regex test for a partial match

if(/Awesome\sProduct<sup>.+</sup>\sDisclosures/i.test(isDisclosure))

Or an index of

if(isDisclosure.indexOf("Awesome Product") >= 0)

Or you could reference the html from a hidden div.

<div id="refs" style="display:none;">
<h3 id="awesome">
    Awesome Product
    <sup>®</sup>
    Disclosures
</h3>
</div>

if(isDisclosure == $("#refs #awesome").html())
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62