-1

I am creating a blog website. I am taking the input using a textarea and storing it in MongoDB. For displaying the data I use: white-space: pre-wrap;.

With pre-wrap if I give the input as:

hello
world

The output is:

hello
world

I want the output as:

hello

world

Code: Taking the input:

<form action="/post/compose" method="post">
    <div class="form-group">
      <label for="postTitle">Title</label>
      <input type="text" class="form-control" name="postTitle" >
      <label for="postBody">Body</label>
      <textarea class="form-control" name="postBody" rows="10" col="10"></textarea>
    </div>
    <button class="btn btn-success" type="submit">Publish</button>
</form>

Storing the input in database:

 const post=new Post({
      Title:req.body.postTitle,
      Content:req.body.postBody,
    
      });
      post.save();

Displaying the input

<div id="fullpost">
<h2 class="text-dark"><%= post.Title %></h2>
<p  class="text-dark" id="Postcontent"><%- post.Content %></p>
</div>

css:

#Postcontent{
  white-space: pre-wrap;
}

Now as I added white-space property if there is \n in the input ,in the output single whitespace is included.But I want two white spaces. Is it possible to do this without regex in Javascript? Please help. Thanks in advance.

1 Answers1

0

Ok so it sounds like what you want is to display line breaks entered in the original text?

In that case, probably you don't want a <textarea>. You might like to use this approach by replacing your textarea with an element (say, a div) with contenteditable=true, and then using a JavaScript function to capture the contents of the div (you won't be able to get it with a standard form submission).

Hugh
  • 371
  • 1
  • 8