Avoid unnecessary spaces in textarea with default text

1

Situation: I have a textarea element that has a default text, which can then be changed by the user. Text has multiple lines.

Problem: The text takes into account the indentation spaces of the source code.

Pseudo-code:

    <html>
        <body>
            <textarea>Olá,
            
            Isto é um teste.</textarea>
        </body>
    </html>

When rendering the page, the text inside the textarea element looks like this:

  

Hello,

     

This is a test.

Is there any way to avoid this?

    
asked by anonymous 31.05.2017 / 20:34

1 answer

3

Via javascript, you could do as follows:

$(function() {
  var textareas = $('textarea.clean');
  $.each(textareas, function(key, value) {
    $(this).val($(this).val().replace(/[ ]+/g, ' ').replace(/^[ ]+/m, ''));
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><textareaclass="clean" rows="5">Olá,

            Isto é um teste.</textarea>

<textarea rows="5">Olá,

            Isto é um teste.</textarea>
    
31.05.2017 / 20:42