Csak Zoli
Csak Zoli

Reputation: 428

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

Upvotes: 9

Views: 30869

Answers (4)

user3510753
user3510753

Reputation: 31

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

this will do the job

Upvotes: 2

<p style="white-space: pre-wrap;"><?php echo $row['description_text'];?></p>

Upvotes: -1

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

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

white-space: pre-wrap;

Upvotes: 30

cbp
cbp

Reputation: 25628

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 />";

Upvotes: 1

Related Questions