By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

How to display TextArea value in <Span> innerHTML with <br> in Netscape

P: n/a
xhe
I met a very headache problem in javascript, I think this might be
difference between IE and NS / Safari.

I have a text area
<form>
<textarea name='tex1' onkeyup='displayit();'></textarea>
</form>
<span id="txtValue"></span>

<script language='javascript'>
function displayit()
{

document.getElementById("txtValue").innerHTML=docu ment.forms[0].tex1.value;
}
</script>

This is a very simple program.
When I run in IE, and no problem, line break in the textares changed
to <brin the span tag, and line changed automatically.
Problem here is when I run it in Netscape and Safari, the line break
in textarea does not change to <br>, and all the text in the textares
are displayed in one paragraph.

Can anyone tell me how to display the multi-line paragraph contents
of the textarea in the <spaninnerHTML correctly to have <br>
there? This is only for NS and Safari. Maybe this is their bugs?

Thanks.
Please email me at he******@gmail.com

Feb 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Feb 7, 3:27 pm, "xhe" <hexuf...@gmail.comwrote:
I met a very headache problem in javascript, I think this might be
difference between IE and NS / Safari.

I have a text area
<form>
<textarea name='tex1' onkeyup='displayit();'></textarea>
</form>
<span id="txtValue"></span>

<script language='javascript'>
function displayit()
{

document.getElementById("txtValue").innerHTML=docu ment.forms[0].tex1.value;}

</script>

This is a very simple program.
When I run in IE, and no problem, line break in the textares changed
to <brin the span tag, and line changed automatically.
Problem here is when I run it in Netscape and Safari, the line break
in textarea does not change to <br>, and all the text in the textares
are displayed in one paragraph.

Can anyone tell me how to display the multi-line paragraph contents
of the textarea in the <spaninnerHTML correctly to have <br>
there?
<script type='text/javascript'>
function displayit() {
document.getElementById("txtValue").innerHTML =
document.forms[0].tex1.value.replace(/\n/g,'<br>');
}
</script>

You may need to test and replace CF or LF characters too, I only
tested quickly in Firefox, IE and Opera.

This is only for NS and Safari.
And Firefox and Opera...

Maybe this is their bugs?
There is no public specification for innerHTML, so simple variances in
behaviour can't be said to be a bug. :-) All you can say is that it
behaves differently in some browsers to others and deal with it (or
don't use it).
--
Rob

Feb 7 '07 #2

P: n/a
xhe wrote:

Hi Xhe,
Can anyone tell me how to display the multi-line paragraph contents
of the textarea in the <spaninnerHTML correctly to have <br>
there?
Why not simply substitute line breaks in your source text by BR tags,
right before writing your innerHTML?

<form action="#">
<textarea onkeyup="foo(this.value)"></textarea>
<span id="txtValue"></span>
</form>
<script type="text/javascript">
function foo(s){
document.getElementById("txtValue").innerHTML=
s.
replace(/&/g,"&amp;").
replace(/</g,"&lt;").
replace(/>/g,"&gt;").
replace(/\r\n|\r|\n/g,"<br>");
}
</script>

(untested on the targeted platform)
Regards,
Elegie.
Feb 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.