Answers for "get text from a js"

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

Browse Popular Code Answers by Language