I'm trying to output a textarea value into a pre (or a div with white-space:pre for that matter). All works well, but it skips the last linebreak. So, if you add text and a linebreak, it won't display the linebreak. If you add text and two linebreaks, it displays text and one linebreak. So, it looks like the text is being trimmed. Can anyone suggest why this is happening and how to solve it?
function starter(e) {
$("#output").text(e);
}
$('#textin').on('input',function(){
starter($('#textin').val())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="textin"></textarea>
<pre id="output" style="border:1px solid black"></pre>
I found an answer after reading When are <br> elements ignored when within a paragraph?.
Browsers take a line break not as a line break, but more like 'end of line'.
With that in mind; this semi-dirty trick does the job, adding an extra line break: