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, '
');
// 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 />
??
` should render it correctly right – kittu Feb 07 '17 at 16:11
/gi, '\n') : "";` – kittu Feb 07 '17 at 16:23
{data.post_details.details}
Sample1
Sample1` and I tried converting to new line like `res.details = res.details.replace(/\s?(
)\s?/g, "\r\n"); self.data.post_details = res;` – kittu Feb 07 '17 at 16:40