Copying text of textarea into div with line breaks

Add a white-space: pre-wrap rule to the div's CSS.

.mas {
    white-space: pre-wrap;
}

Demo: http://jsfiddle.net/Pqygp/13/


Use this line: Fiddle

$('.'+contentAttr+'').html(value.replace(/\n/g,"<br>"));

The problem was that newlines don't create linebreaks in html, but <br> will.


You need to convert the literal newlines into <br> tags for proper output in the DIV.

$('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>'));

Shown in your code below:

    $('.content:not(.focus)').keyup(function(){					
                                    
                                    
        var value = $(this).val();
        var contentAttr = $(this).attr('name');
        
        $('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>')); //convert newlines into <br> tags
        
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea name="mas" rows="15" class="content"></textarea> <p>&nbsp;</p> <div class="mas" >Texts Comes here</div>

JSFiddle