get text from html string javascript

Solutions on MaxInterview for get text from html string javascript by the best coders in the world

showing results for - "get text from html string javascript"
Reilly
19 Sep 2017
1<input id="input" />
2<div id="div" contenteditable="true">Text in DIV</div>
3<p id="p" contenteditable="true">Text in paragraph</p>
4<script type="text/javascript">
5  // function to get text from HTML element
6  function getText(id) {
7    // if element is input then return the value
8    if (document.getElementById(id).nodeName === "INPUT") {
9      return document.getElementById(id).value;
10    }
11    // otherwise return the textContent
12    return document.getElementById(id).textContent;
13  }
14  // print the current values
15  console.log(getText("div"));
16  console.log(getText("p"));
17</script>