Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep line breaks when using .text() method for jQuery?

I'm trying to achieve a typewriting effect and my content consists of a lot of HTML entities. The problem with using .html() is that since it writes out each letter at a time, it will type out & then l then t then ; and finally it would change to <.

HTML

<p id="src"> &lt;!DOCTYPE html&gt; &lt;!--[if lt IE 7]&gt;      &lt;html class=&quot;no-js lt-ie9 lt-ie8 lt-ie7&quot;&gt; &lt;![endif]--&gt; &lt;!--[if IE 7]&gt;         &lt;html class=&quot;no-js lt-ie9 lt-ie8&quot;&gt; &lt;![endif]--&gt; &lt;!--[if IE 8]&gt;         &lt;html class=&quot;no-js lt-ie9&quot;&gt; &lt;![endif]--&gt; &lt;!--[if lt IE 9]&gt;      &lt;html class=&quot;no-js lt-ie9&quot;&gt; &lt;![endif]--&gt; </p> <p id="typed-paragraph">     <span id="target"></span>     <span id="typed-cursor">|</span> </p> 

CSS

#src {     display: none; } 

jQuery

(function(){     var srcText = $("#src").html();         i = 0;         result = srcText[i];     setInterval(function() {         if(i == srcText.length-1) {             clearInterval(this);             return;         };         i++;         result += srcText[i].replace("\n", "<br />");         $("#target").html(result);     }, 150); // the period between every character and next one, in milliseonds. })(); 

You can see the example of it here http://jsfiddle.net/j9KF5/9/

But if I use .text() then I lose all the line breaks.

Ultimately, how I can either fix the entities problem or the line break problem?

like image 921
anita Avatar asked Mar 27 '14 04:03

anita


People also ask

How do I preserve line breaks when getting text from a textarea?

To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n' . const post = document. createElement("p"); post. textContent = postText; post.

Is text () a jQuery method?

jQuery text() MethodThe text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.

How do you preserve all spaces and linebreaks in HTML?

The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

How can add break tag in jQuery?

You can use . after() to insert an element after each the selector matched, like this: $("#twitter_update_list span"). after("<br />");


2 Answers

You don't need to worry about the HTML entities nor any complex string replacing.

All you need is a little CSS:

#target {     white-space: pre; } 

and use the .text() approach:

(function(){     var srcText = $("#src").text().trim();         i = 0;         result = srcText[i];     setInterval(function() {         if(i == srcText.length-1) {             clearInterval(this);             return;         };         i++;         result += srcText[i];         $("#target").text(result);      }, 150); // the period between every character and next one, in milliseonds. })(); 

http://jsfiddle.net/mattball/vsb9F/

like image 162
Matt Ball Avatar answered Sep 22 '22 08:09

Matt Ball


To anyone who's looking for what the title of the question actually asks (this is how I got to this page, "how to keep line breaks when using jQuery's text() method"), you can use something like this:

(function($){    $.fn.innerText = function(msg) {          if (msg) {             if (document.body.innerText) {                for (var i in this) {                   this[i].innerText = msg;                }             } else {                for (var i in this) {                   this[i].innerHTML.replace(/&amp;lt;br&amp;gt;/gi,"n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");                }             }             return this;          } else {             if (document.body.innerText) {                return this[0].innerText;             } else {                return this[0].innerHTML.replace(/&amp;lt;br&amp;gt;/gi,"n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");             }          }    }; })(jQuery); 

Now call $('.some-class').innerText() to get the text with line breaks preserved.

like image 44
orcaman Avatar answered Sep 20 '22 08:09

orcaman