JavaScript innerHTML character code match

Associate
Joined
28 Nov 2005
Posts
431
Location
Scotland
Hi,

In my javascript code I am checking for a match of the inner HTML of a anchor tag which subsequently, if it matches, the inner HTML will be changed.

Normally this works perfectly, however this time I have a HTML chracter code in the inner HTML of the anchor tag and it would appear that javascript cannot match or read HTML character codes?

What I have is:

JavaScript
Code:
var nme = this.id;
	
var d = document.getElementById(nme);
	
if(d.innerHTML == "Show Description »")
{

d.innerHTML = "« Hide Description"; 

}


HTML
Code:
<a id="hideshow" href="#">Show Description &raquo;</a>


If I remove the HTML character code &raquo; (for » btw) in the HTML and the JS it works perfectly and if I replace the character code in the JS with » it still doesnt work.

It is almost like JS cannot detect character codes in the HTML or JS is reading the renderd page yet still doesnt understand the » character.

Can anyone help?

Cheers
 
JS uses the unicode value as opposed to the HTML entity name. Try this:

Code:
var nme = this.id;	
var d = document.getElementById(nme);
	
if(d.innerHTML == "Show Description \xBB") {
  d.innerHTML = "\xAB Hide Description"; 
}

Ah thanks for that :) never knew that, works a treat, thanks :)
 
Back
Top Bottom