populating a textarea with special characters
Solution 1
Inside a textarea
, you need to convert the following characters into their HTML entities:
& => &
> => >
< => <
That way, 
would become &#5
. Visually, to the user, it would remain 
.
You are not specifying the server side language you're using. In PHP, the correct function would be htmlspecialchars()
Solution 2
escape the & as &
Comments
-
Nathan almost 2 years
I'm populating a textarea with previous input of a user. This is pulled from a database and set as the content of the textarea server side.
It seems we are having an issue with a typo and a combination of special characters. if the user inputs

originally, when I try to populate my textarea with that it just renders a little square like its interpreting the character encoded value.Creating a HTML file with the following demonstrates my issue.
<textarea name"mytextarea">some text  some more text </textarea
this is a typo, the user intended to enter #5 & #6 so a fix for this is simply to ensure when the user puts an ampersand in that I have a space on either side of it before I display it in the textarea. Its just a special character issue backwards from what i'm use to seeing.
I'm curious if there is a way to get the text area to display the characters like the user typed it and preserve that through form submission. To save the over head of having to parse or html encode the text before putting into the textarea.
Thanks, Muchly
-
Nathan over 13 yearsI'll try to set up a test to verify this but if converted to html enttities then copied to another textarea via javascript then submitted would the page processing the form receive  or $amp;#6 ? That is where I'm likely to run into an issue.
-
Joe White over 13 yearsIf your HTML contains
<textarea>&#6</textarea>
, then the user would see a textbox that contains the text
, and if they submit, the server would receive the text
. That's just how you have to specify an ampersand character in your page, as opposed to an escape character that begins an HTML entity.