IVlad
IVlad

Reputation: 43477

Escaping html entities in textarea

I have a textarea used for comments that loads the contents of a div when a link is pressed, putting those contents in the displayed textarea.

Everything worked fine until I introduced a way to display interpoints (I'm talking about the · entity). These display fine in the div, but the problem is they also display as dots in the actual textarea, which I don't want. I want them to display as {*} in the textarea, since this is the string that gets converted to · on the server side.

This is the function I use to handle the display of tags:

function HtmlDecode(str) 
{
    var ta = document.createElement("textarea");
    ta.innerHTML = str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&middot;/g, "{*}");
    toReturn = ta.value;
    ta = null;

    return toReturn.replace(/<br\s*\/?>/mg, "\n");
}

I output the textarea like this:

anotherDiv.innerHTML = '<textarea ...>' + HtmlDecode(div.innerHTML) + '</textarea>';

What I don't understand is why if I change the /&middot;/g replace with /asdf/g for example, any asdf will get replaced just fine.

Bottom line: how can I make my textarea display {*} instead of an actual interpoint?

Upvotes: 0

Views: 2180

Answers (1)

Ernesto
Ernesto

Reputation: 1592

What about .replace(/·/g, "{*}") instead of .replace(/&middot;/g, "{*}");?

Upvotes: 1

Related Questions