admin管理员组

文章数量:1180476

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.

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.

Share Improve this question edited Jan 5, 2013 at 22:16 Csak Zoli asked Jan 5, 2013 at 21:59 Csak ZoliCsak Zoli 4281 gold badge4 silver badges11 bronze badges 7
  • 1 isn't enter key already creating a new line in a textarea? – keune Commented Jan 5, 2013 at 22:01
  • in the textarea yes but i want it to create in the paragraph called arsh ID – Csak Zoli Commented Jan 5, 2013 at 22:02
  • What does your HTML look like? Can you make a Fiddle? – HellaMad Commented Jan 5, 2013 at 22:02
  • This is a very small part of the code but its not working here – Csak Zoli Commented Jan 5, 2013 at 22:03
  • @CsakZoli It's working for me. Inspect the paragraph and you will see the newlines. jsfiddle.net/afgtB – HellaMad Commented Jan 5, 2013 at 22:06
 |  Show 2 more comments

4 Answers 4

Reset to default 30

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

white-space: pre-wrap;
document.getElementById('arsh').innerHTML=document.getElementById("area").value.replace(/\n/g,'<br />')

this will do the job

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

本文标签: javascriptNew line in paragraph when Enter is pressed in textareaStack Overflow