0

My process so far is:

  1. Take text input into a form's textarea (often, an email with spaces, linebreaks, and perhaps special characters)

    <label for="sample">Sample:</label><br>
    <textarea id="sample" rows="4" cols="50" style="white-space: pre;">
    
  2. Output it in the rendered webpage using javascript.

    function generate() { 
    
      var sample = document.getElementById("sample").value;
      //get the pasted in text with spaces and linebreaks from the text area
    
      var message = "<p><br> <b>Sample text:</b> More text, html formatting " + sample + " </p>";
      //make a variable with html formatted text and the sample text
    
      document.getElementById("pagetext").innerHTML = message;
      //show the message var text by overwriting the "pagetext" placeholder on the screen when user clicks button.
    
    }
    

Right now spacing and line breaks are not kept.

For example with Input \nText

The output is: Text

How do I keep the spacing and formatting while using HMTL text areas and Javascript?

2
  • This should help: stackoverflow.com/questions/10216805/… Commented Apr 21, 2020 at 20:34
  • If you are wanting to preserve multiple spaces and line breaks, what about the <pre> tag? If you could place your output in a <pre> tag then it should preserve what you want preserved. Commented Apr 21, 2020 at 20:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.