Reputation: 21160
I have a text area that looks like this:
<textarea id="txtBody" name="txtBody">q2312312312<strong>313213213</strong>21231321231231 < &lt;</textarea>
When I do this in javascript:
var t = document.getElementById('txtBody').value;
I get this back:
<p>q2312312312<strong>313213213</strong>21231321231231 < <</p>
Seems like it should just give me the exact characters as they are... am I missing something? Is there a way to get the exact characters out of a text area?
Upvotes: 1
Views: 11075
Reputation: 943999
am I missing something?
&
is HTML for "Start an entity".
When you put the text into the HTML document (between the start and end tags for the textarea), you are failing to represent that text as HTML.
As a result, the browser performs error recovery and treats <strong>
as "A less than character, the word strong, then a greater than character" (because tags are not allowed inside a textarea
element, but treats <
as "A less than character" because there isn't a machine detectable error here.
Is there a way to get the exact characters out of a text area?
You need to write the HTML correctly in the first place.
<textarea id="txtBody" name="txtBody">q2312312312<strong>313213213</strong>21231321231231 &lt; &amp;lt;</textarea>
Upvotes: 4
Reputation: 12415
I get the right contents, I guess your (X)HTML is not valid...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.19.1" />
</head>
<body>
<textarea id="txtBody" name="txtBody">q2312312312<strong>313213213</strong>21231321231231& < &lt;</textarea>
<script>
var t = document.getElementById('txtBody').value;
alert(t);
</script>
</body>
</html>
Upvotes: 1