Preserve line break when Replicating val () content in span

1

Good afternoon, in line with this topic - https: //en.stackoverflow.com/questions/203969/replicate-control-of-val-em-span I have the following code

$('textarea.skills_knowledge').blur(function() {
    var skills_knowledge = $(this).val();
    $('span.skills_knowledge').html(skills_knowledge); });

I would like to know how to preserve the line breaks, ie if you hit the enter in the text area, appear this in the span properly one below the other

I do not know if it is the best format, I accept suggestions for improvement, but I found this format in topic -https: //en.stackoverflow.com/questions/40803/use-of-val-no-script-me-do-lead-shocks of-line? rq = 1

$('textarea.skills_knowledge').blur(function() {
    var skills_knowledge = $(this).val();
    var skills_knowledge = skills_knowledge.replace(/\r?\n/g, '<br />');
    $('span.skills_knowledge').html(skills_knowledge); });
    
asked by anonymous 12.05.2017 / 18:50

1 answer

2

This code will replace all \n line breaks in <br/> tags, combinations are meant to work across multiple browsers.

$('textarea.skills_knowledge').blur(function() {
  var skills_knowledge = $(this).val();
  skills_knowledge = skills_knowledge.replace(/\r\n|\r|\n/g, "<br/>");
  $('span.skills_knowledge').html(skills_knowledge);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaclass="skills_knowledge"></textarea>

<span class="skills_knowledge"></span>
    
12.05.2017 / 19:05