Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textarea line break jquery

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

like image 219
Viktors Avatar asked Aug 22 '12 18:08

Viktors


1 Answers

<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

like image 68
epascarello Avatar answered Sep 30 '22 01:09

epascarello