Thursday, May 28, 2020
Extract only text content from HTML in Javascript
1. Create a temporary DOM element and retrieve the text
/* Returns the text from a HTML string */ function GetPlanText_FromHtml(html){ // Create a new div element var tmp = document.createElement("div"); // Set the HTML content with the providen tmp.innerHTML = html; // Retrieve the text property of the element (cross-browser support) return tmp.textContent || tmp.innerText || ""; } var myHTML_String= "
Hello World
This is Extracted string from HTML
"; //Hello World //This is Extracted string from HTML console.log(GetPlanText_FromHtml(myHTML_String));
2. using jQuery
var myHTML_String= "
Hello World
This is Extracted string from HTML
"; var HTML2Text= $("
").html(myHTML_String).text();
Newer Posts
Older Posts
Home
View mobile version
Subscribe to:
Posts (Atom)