Blease
Blease

Reputation: 1420

Javascript copy text without tags

I have a script that when clicked will copy the contents of a div to the clipboard

<script type="text/javascript">
var program=document.getElementById('code');
ShowLMCButton(program.innerHTML, '', '', './static/js/lmcbutton.swf');
</script>

But when I click "Copy" it copies

<p>Line 1<br />Line 2</p>

Is there anyway to copy it like this:

Line 1 Line 2

Upvotes: 1

Views: 1060

Answers (3)

adripanico
adripanico

Reputation: 1058

You are using an external library, so you should refer to the library documentation.

Anyway, there is a related post here.

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71939

You can use textContent or innerText. The differences, according to MDN, are:

  • Note that while textContent gets the content of all elements, including <script> and <style> elements, the mostly equivalent IE-specific property, innerText, does not.
  • innerText is also aware of style and will not return the text of hidden elements, whereas textContent will.
  • As innerText is aware of CSS styling, it will trigger a reflow, whereas textContent will not.

Upvotes: 1

MK.
MK.

Reputation: 34597

Sounds like you want the innerText instead of innerHTML?

Upvotes: 2

Related Questions