& showing as & in textfield

After making an ajax call I show the results under a textfield to the user. And to make sure the charset is correct I called the php function utf8_encode to make sure whatever was sent back with ajax had the same charset as the webpage. So everything works fine, including special characters, until the user hovers over one of the results. What is supposed to happen is that the result will become the textfield value. Unfortunately the & character is being converted to & in the textfield. I tried unescaping it, but it still does not work. Here is my code:

Brandfield is the textfield i want to change the value for. This shows the ajax call and the function to change the value of the textfield.

function hoverBrand(change)
{
	change.style.backgroundColor="#FFFF00";
	brandfield.value=unescape(change.innerHTML);
}

function checkBrands(brandval, event)
{	
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
   if(KeyID==9)
	{
		removeHtml();
		return;
	}
	var whichbrand=brandval;
	brandfield=whichbrand;
	if(whichbrand.value.length>1)
	{
		var xmlhttp=false;
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e)
		{
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e)
			{
				xmlhttp=false;
			}
		}
		if(!xmlhttp && typeof XMLHttpRequest!='undefined')
		{
			xmlhttp=new XMLHttpRequest();
		}
		var toPage='async.php?brandname='+whichbrand.value;
		xmlhttp.open("GET", toPage);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText.length>2)
				{
					var xval=findPosX(whichbrand);
					var yval=findPosY(whichbrand);
					yval+=22;
					xval+=3;
					document.getElementById('movablebox').style.position='absolute';
					document.getElementById('movablebox').style.top=yval+'px';
					document.getElementById('movablebox').style.left=xval+'px';
					document.getElementById('movablebox').style.zIndex='3';
					document.getElementById('movablebox').visibility='visible';
					document.getElementById('movablebox').innerHTML=xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
	}
	else
	{
		removeHtml();
	}
}

Hi,

It’s not inescaping you’re looking for. & is the HTML encoded version of the ampersand.
I’m not familiar with html encoding/decoding within javascript so i’d suggest just doing manual decoding where you know there are problems.


innerHTML.replace('&', '&');

Hope this can help you to encode/decode in JavaScript though I haven’t tested it yet.
http://www.strictly-software.com/htmlencode

Thanks I’ll check this out.