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"> <!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if lt IE 9]> <html class="no-js lt-ie9"> <![endif]--> </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?
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.
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.
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.
You can use . after() to insert an element after each the selector matched, like this: $("#twitter_update_list span"). after("<br />");
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/
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(/&lt;br&gt;/gi,"n").replace(/(&lt;([^&gt;]+)&gt;)/gi, ""); } } return this; } else { if (document.body.innerText) { return this[0].innerText; } else { return this[0].innerHTML.replace(/&lt;br&gt;/gi,"n").replace(/(&lt;([^&gt;]+)&gt;)/gi, ""); } } }; })(jQuery);
Now call $('.some-class').innerText() to get the text with line breaks preserved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With