1

I am using text area to enter text and on form submit, the text displayed in the view is not retaining any line breaks and spaces.

Text area:

<textarea type="text" id="topicDetails"></textarea>

Tried replacing text using the following:

postTopic(){
    var content = document.getElementById('topicDetails').value;
//            textcontent = content.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ "" +'$2'); 
//            textcontent = content.replace("\r\n", "\\r\\n"); 
//            textcontent = content.replace(/\r?\n/g, '<br />');
//            textcontent = content.replace(/\r?\n/g, '&#13');
//            var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
//            textcontent = (content + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
    textcontent = content.replace(/\n/g,"<br>")

    var topic = {
        "topicId" : new Date().getTime(),
        "title": document.getElementById('title').value,
        "details": textcontent,
        "username": DataMixin.data.username,
        "userImage": "assets/img/Logos Divine Light/6.png",
        "dayPosted": new Date().toLocaleString()
    }

    console.log('posting blog..... ', topic);
    self.data.blogTopicsArr.push(topic);

    $.ajax({
        url: "/new_topic",
        type: "POST",
        data: JSON.stringify(self.data.blogTopicsArr),
        contentType: "application/json",
        success: function (res) {
            console.log('res is ', res);
            if (res == 'Authentication failed'){
                self.data.blogTopicsArr.splice( - 1, 1);
                self.update(self.data.blogTopicsArr);
                riot.route("signup_popup");
            } else if (res == 'saved'){
                console.log('blog posted successfully: ', self.data.blogTopicsArr);
                document.getElementById('title').value = '';
                document.getElementById('topicDetails').value = '';
                self.update();
            } else if (typeof res.redirect == 'string'){
                console.log('res.redirect ', res.redirect);
                riot.route(res.redirect)
            }
        },
        error: function (err) {
        console.log('err>>>>', err);
        }
    });
    $('#myModal').modal('hide');
}

Tried three different ways with no luck.

The third approach gives the output with <br /> tags. How do I preserve new lines?

Output is:

Lorem Ipsum is simply dummy text.<br /> Industry's standard text ever since the 1500s, <br /><br />Why do we use it?<br />It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. <br /><br />

Update:

After successful form post, I am updating the view using riot's self.update(); or this.update()

Update2

Actually I am sending the form data to database and fetching the text back from database to render it. But the text sent to DB has <br> tag inserted before saving it in DB so why it displays text as Lorem Ipsum is simply dummy text.<br /> Industry's standard text ever since the 1500s, <br /> ??

kittu
  • 6,662
  • 21
  • 91
  • 185

4 Answers4

4

Actually I all had to was add pre-wrap like below.

<p style="white-space: pre-wrap;">{data.post_details.details}</p>

This retained the spacing with new lines as well.

kittu
  • 6,662
  • 21
  • 91
  • 185
1

You can just replace the newlines with <br> using this .replace(/\n/g,"<br>")

function postTopic() {
  var content = document.getElementById('topicDetails').value;

  var textcontent = content.replace(/\n/g,"<br>");

  var topic = {
    "details": textcontent,
  }
}
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
0

You could simply handle this when you process the form on the server side. For instance, if your form action is set to "companyForm.php" then in your companyForm.php you could use a function like nl2br, such as:

$companyName = nl2br($_POST['companyName']);

http://php.net/manual/en/function.nl2br.php

If for whatever reason you absolutely must do this on the client side, you could use nl2br on php.js by adding this:

function nl2br (str, is_xhtml) {

    if (typeof str === 'undefined' || str === null) {
        return ''
    }

    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

Then use:

nl2br(textcontent, false)
Michael Rodriguez
  • 2,142
  • 1
  • 9
  • 15
0

You want to use

&#10; as \r and

&#13; as \n

Plug those values into your replace function and it should work.

More info:

Community
  • 1
  • 1
Simon Visser
  • 364
  • 3
  • 14
  • Like `content.replace(/\r?\n/g, ' ');` – kittu Feb 07 '17 at 15:44
  • and it prints in the output – kittu Feb 07 '17 at 15:45
  • Updated the question. Actually I am fetching the text from DB but it renders with `
    ` tags inside the text when I display it on view(html page). I just checked the DB, it contain text like `asdasd
    asd
    asd


    asd`
    – kittu Feb 07 '17 at 16:18