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

X

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=document.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 <br> in 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 <span> innerHTML correctly to have <br>
there? This is only for NS and Safari. Maybe this is their bugs?

Thanks.
Please email me at (e-mail address removed)
 
R

RobG

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=document.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 <br> in 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 <span> innerHTML 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).
 
E

Elegie

xhe wrote:

Hi Xhe,
Can anyone tell me how to display the multi-line paragraph contents
of the textarea in the <span> innerHTML 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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top