Take the 2-minute tour ×

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') {

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

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

<br> are html breaks. You want \n in a textarea.


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>");

JSFiddle Example

share|improve this answer
May I ask you how to do textarea heigh for text height. How to get text height and after pass this value to textarea height. I tried this .html('<textarea style="height: 400px;">'+jQuery(this).html().replace(/<br>/gi,"\n")+'</textarea>') and it works but now height is static but I want to do dynamic –  Viktors Aug 22 '12 at 19:06
what does height value relate too? –  epascarello Aug 22 '12 at 19:12
today I noticed that br now safe good but spaces not in view...is it possible to make many spaces vissible? –  Viktors Aug 23 '12 at 19:17
maybe you can show in your example on JSFiddle how to keep spaces (more then one) and line breaks (this already work) –  Viktors Aug 23 '12 at 19:34
You would have to replace spaces with &nbsp; –  epascarello Aug 23 '12 at 19:47
add comment

Your Answer


