Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line in paragraph when Enter is pressed in textarea

document.getElementById('area').onkeyup=keydown;
function keydown (event) {
if (event.target.id=='area' && event.which==13) {
document.getElementById('arsh').innerHTML+="\n";
     }
}

Arsh is the ID of my paragraph where i want to add the new line why is this not working can anyone help me ?I just want to add a new line when Enter is pressed in the textarea (ID area) to my Paragraph (ID arsh).

here is the HTML

<textarea ID="area" class="in">
Message
</textarea>
<br />
<br />
<p ID="tiesh" class="sh">
</p>
<br />
<br />
<p ID="arsh" class="sh">
</p>
<br />
</div>

I am basically trying to do it like here in stackoverflow when you press enter in the textarea down the paragraph jumps down a line also.

enter image description here

like image 763
Csak Zoli Avatar asked Jan 05 '13 21:01

Csak Zoli


3 Answers

Using CSS, give #arsh (or any other elements that you want to render new lines) this attribute.

white-space: pre-wrap;
like image 173
Austin Brunkhorst Avatar answered Oct 26 '22 14:10

Austin Brunkhorst


document.getElementById('arsh').innerHTML=document.getElementById("area").value.replace(/\n/g,'<br />')

this will do the job

like image 31
user3510753 Avatar answered Oct 26 '22 12:10

user3510753


The newline character (\n) do not render as new lines on the screen. You probably need to add an HTML break instead:

document.getElementById('arsh').innerHTML+="<br />";
like image 1
cbp Avatar answered Oct 26 '22 13:10

cbp