I’m doing something very similar to the stackoverflow question preview only much more basic.
user types in text area -> keyup shows what they’ve typed in preview
new lines aren’t working
$('input, textarea').keyup(function(){
var value = $(this).attr('value').replace('\n', '<br />').replace('\r', '<br />');
$('p.preview').html(value);
})
You need something like this:
Note the
/g,which you need to replace more than the first occurance, and we’re replacing only one of the returns, so you don’t get 2 line returns in your preview per 1 in the textarea/input.