HTML tags in textarea

X

Xerxes

Hi,
I have a textarea in my page and its value comes from the database.
The text in the database table contains some html tags like <br />.
However, when the page is rendered I see <br /> instead of line breaks
and view source shows "&lt;br /&gt;". I tries to do a replace in the
js code that assigns the value to textarea but no change.
When I added a breakpoint, at the point of assignment, the value
contained < and > and not &lt; and &gt; Is there anything I can do to
force the html tags to be interpretted and not printed?

document.getElementById('taComments').value =
record.Comment.replace('&lt;', '<').replace('&gt;', '>');

"record.Comment" conatins:
This comment was added manually in the database for testing
purposes.<br />Some other text follows ....
 
E

Evertjan.

Xerxes wrote on 26 mei 2010 in comp.lang.javascript:
Hi,
I have a textarea in my page and its value comes from the database.
The text in the database table contains some html tags like <br />.

That is an XML tag, not an HTML one.
However, when the page is rendered I see <br /> instead of line breaks
and view source shows "&lt;br /&gt;". I tries to do a replace in the
js code that assigns the value to textarea but no change.
When I added a breakpoint, at the point of assignment, the value
contained < and > and not &lt; and &gt; Is there anything I can do to
force the html tags to be interpretted and not printed?

document.getElementById('taComments').value =
record.Comment.replace('&lt;', '<').replace('&gt;', '>');

try:

==========================================

<textarea id='ta'>here</textarea>

<script type='text/javascript'>
var oldT = 'Hello<br />world!';
var newT = oldT.replace('<br />','\n');
document.getElementById('ta').innerHTML = newT;
</script>

==========================================
 
X

Xerxes

Xerxes wrote on 26 mei 2010 in comp.lang.javascript:


That is an XML tag, not an HTML one.



try:

==========================================

<textarea id='ta'>here</textarea>

<script type='text/javascript'>
  var oldT = 'Hello<br />world!';
  var newT = oldT.replace('<br />','\n');
  document.getElementById('ta').innerHTML = newT;
</script>

==========================================

Thank you for the reply.
I modified the script as you suggested but it gives me an error when I
change '.value' to '.innerHTML':
htmlfile: unknown runtime error.

When I changed it back to '.value' it kind of works; only the first
instance of <br /> is replaced by '\n' which actually does a line
break. Other <br /> remain unchanged.
 
E

Evertjan.

Xerxes wrote on 26 mei 2010 in comp.lang.javascript:

[please do not quote signatures on usenet]
Thank you for the reply.
I modified the script as you suggested but it gives me an error when I
change '.value' to '.innerHTML':
htmlfile: unknown runtime error.

When I changed it back to '.value' it kind of works; only the first
instance of <br /> is replaced by '\n' which actually does a line
break. Other <br /> remain unchanged.

I am not going to do your work, just point you to a useful direction.

Please readup the specs of javascript replace()
on regex and replace multiple.
 
X

Xerxes

Xerxes wrote on 26 mei 2010 in comp.lang.javascript:


That is an XML tag, not an HTML one.



try:

==========================================

<textarea id='ta'>here</textarea>

<script type='text/javascript'>
  var oldT = 'Hello<br />world!';
  var newT = oldT.replace('<br />','\n');
  document.getElementById('ta').innerHTML = newT;
</script>

==========================================

I found this piece of code at http://www.bennadel.com/blog/142-Ask-Ben-Javascript-String-Replace-Method.htm.
It allowed me to replace all instances.
Thank you for yoour help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top