Markos Gogoulos
Markos Gogoulos

Reputation: 381

jquery autocomplete: how to handle special characters (& and ')?

While using jQuery's autocomplete, I've noticed that characters & and ' are escaped as & and '

Example, autocomplete displays Barbara’s Straight Son but when I choose this the field shows Barbara's Straight Son.

Any ideas how I can avoid this?

Thank you for your time!

Upvotes: 1

Views: 3210

Answers (2)

Rathore
Rathore

Reputation: 1988

For example you can set label value in autocomplete using this code

var temp = document.createElement("pre"); // create new DOM element 
temp.innerHTML = "Barbara's Straight Son" ; // set string which content HTML entities 
return {
            label: temp.firstChild.nodeValue, // get string from that DOM element which replace HTML entities to special character 
            value: "" // set your value here
}

Upvotes: 0

mkoryak
mkoryak

Reputation: 57958

you need to unescape the html entities. the easiest way i found to do that is to use this function:

var unescapeHtml = function (html) {
      var temp = document.createElement("div");
      temp.innerHTML = html;
      var result = temp.childNodes[0].nodeValue;
      temp.removeChild(temp.firstChild)
      return result;
  }

so pass your string through this function before it gets put into your input box. You might have to modify the autocomplete plugin in order to do this.

Upvotes: 2

Related Questions