Answers for "get plain text from html javascript"

3

javascript remove html from text

//remove html tags from a string, leaving only the inner text
function removeHTML(str){ 
    var tmp = document.createElement("DIV");
    tmp.innerHTML = str;
    return tmp.textContent || tmp.innerText || "";
}
var html = "<div>Yo Yo Ma!</div>";
var onlyText = removeHTML(html); "Yo Yo Ma!"
Posted by: Guest on July-31-2019
0

get text from html string javascript

<input id="input" />
<div id="div" contenteditable="true">Text in DIV</div>
<p id="p" contenteditable="true">Text in paragraph</p>
<script type="text/javascript">
  // function to get text from HTML element
  function getText(id) {
    // if element is input then return the value
    if (document.getElementById(id).nodeName === "INPUT") {
      return document.getElementById(id).value;
    }
    // otherwise return the textContent
    return document.getElementById(id).textContent;
  }
  // print the current values
  console.log(getText("div"));
  console.log(getText("p"));
</script>
Posted by: Guest on July-16-2021

Code answers related to "get plain text from html javascript"

Browse Popular Code Answers by Language