title: Convert HTML to plain text in contentEditable
body: <p>I have a <code>contentEditable</code> and I strip the formatting of pasted content <code>on('paste')</code> by catching the event. Then I focus a textarea, paste the content in there, and copy the value. Pretty much the answer from <a href="https://stackoverflow.com/a/10551358/1959872">here</a>. The problem is that I can’t do this:</p>
  
  <pre><code>$("#paste-output").text($("#area").val());
  </code></pre>
  
  <p>because that would replace my entire content with the pasted text. So I need to paste the content at caret position. I put together a script that does that:</p>
  
  <pre><code>pasteHtmlAtCaret($("#area").val());
  
  // pastes CTRL+V content at caret position
  function pasteHtmlAtCaret(html) {
    var sel, range;
    if (window.getSelection) {
      sel = window.getSelection();
      if (sel.getRangeAt &amp;&amp; sel.rangeCount) {
        range = sel.getRangeAt(0);
        range.deleteContents();
        var el = document.createElement("div");
        el.innerHTML = html;
        var frag = document.createDocumentFragment(), node, lastNode;
        while ((node = el.firstChild)) {
          lastNode = frag.appendChild(node);
        }
        range.insertNode(frag);
  
        if (lastNode) {
          range = range.cloneRange();
          range.setStartAfter(lastNode);
          range.collapse(true);
          sel.removeAllRanges();
          sel.addRange(range);
        }
      }
    } else if (document.selection &amp;&amp; document.selection.type != "Control") {
      document.selection.createRange().pasteHTML(html);
    }
  }
  </code></pre>
  
  <p>The only problem is that it pastes HTML content, at caret position using the <code>html</code> variable. How can I transform that into plain text? I tried adding the jQuery <code>.text(html)</code> to variable without luck. Something like this might help:</p>
  
  <pre><code>el.textContent||el.innerText;
  </code></pre>
  
  <p>Any ideas or a better solution? Thanks!</p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  Thanks to the answers below I modified my code and solved the issue. I basically copied the value of <code>textarea</code> into a <code>div</code> and grabbed only its <code>.text()</code>:</p>
  
  <pre><code>// on paste, strip clipboard from HTML tags if any
  $('#post_title, #post_content').on("paste", function() {
      var text = $('&lt;div&gt;').html($("#area").val()).text();
      pasteHtmlAtCaret(text);
    }, 20);
  });
  </code></pre>