Need Help for.
I have one texarea which i have write keyword then press Enter key result with <BR> generate.

$("#chat_box").keydown(function (e){
    if(e.keyCode == 13){                            
    var textval = $("#chat_box").val();
    alert(textval+' you pressed enter ^_^');
    }
    });


    ENTER : Hello,
           test

   OUTPUT: Hello, test (That not correct)

Enter Key works perfectly.
I have also try it Append()
Suggestion me.

Thank you

Dani AI

Generated

The textarea stores literal newline characters when Enter is pressed, so inserting its value directly into normal HTML will collapse those breaks. was on the right track: the solution is either to convert newlines into <br> tags (after escaping) or to let CSS preserve the line breaks. The two approaches below are safer and cover Windows CRLF as well as plain LF.

A safe convert-and-insert method (escape first to avoid XSS, then convert CRLF/LF to <br>):

var raw = $('#chat_box').val();
var escaped = $('<div/>').text(raw).html();     // escape any HTML
var html = escaped.replace(/\r?\n/g, '<br>');    // handle CRLF and LF
$('#output').html(html);                         // insert as HTML

An alternative that avoids producing HTML completely: output the text and use CSS to preserve whitespace and line breaks. This is often preferable for displaying plain user input.

/* CSS */
#output { white-space: pre-wrap; }

/* JS */
$('#output').text($('#chat_box').val());

Troubleshooting notes: check for carriage returns from pasted Windows text (use the \r?\n regex), prefer keydown for Enter detection when intercepting the key, and call e.preventDefault() if Enter should submit instead of inserting a newline (use e.shiftKey to allow Shift+Enter for newlines). If literal "&lt;br&gt;" shows up in the page, the string is being escaped after replacement — make sure the escaped/raw handling and the final use of .html() or .text() match the chosen approach. This complements 's replace idea by adding escaping, CRLF handling, and a safer CSS option.

Member Avatar for Member #120589

The textarea is probably storing '\n' (newline) not '<br />'.

You could try...

textval = textval.replace(/(\n)+/g, '<br />');
commented: Thank you. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.