textarea line break jquery
javascript, jquery, jquery-ui
Solution
`<br>` are html breaks. You want `\n` in a textarea.
jQuery(this).html().replace(/<br>/gi,"\n")
When you save, you want to change it back to breaks and use html() instead of text();
var elem = jQuery(this);
if (id === 'commedit') {
var text = jQuery(this).html().replace(/<br>/gi,"\n");
elem.html('<textarea>'+ text +'</textarea>');
} else if (id === 'commsave') {
var html = elem.find('textarea').val().replace(/\n/g,"<br>");
elem.html(html);
}
JSFiddle Example
Problem
I have got a button 'edit description' when I click on it text disappear and appear ``` if (id == 'commedit') jQuery(this).html('<textarea>'+jQuery(this).text()+'</textarea>'); else if (id == 'commsave') { jQuery(this).text(jQuery(this).find('textarea').val()); } ``` In MySql I have this text - `"tetee<br>afafaf<br>afafaf<br>afsafsasfasf"` in view it dispays with line breaks but when I click 'edit' in text area which come with Jquery text appear without lines and when I click save it also appear in my description field in one long line without line breaks. So I need your help