0

I need to select my HTML content ( as in highlight text so I can copy it). I want to create a button that selects all content between two <div> or two <p>(any tag really). Check out codepen for what I need to select.

I have found some selection ideas, but I am not sure (kinda confused)

<input onClick="this.setSelectionRange(0, 9999)" value="Sample Text" />
var range = document.createRange();
var sel = window.getSelection();
range.setStart(el.childNodes[2], 5);
range.collapse(true);

I want it to create range by a two tags.
Thanks for any help

Oliver Nybroe
  • 1,828
  • 22
  • 30

1 Answers1

0

According to Denis from Select all DIV text with single mouse click you should use

function SelectText(element) {
 if (document.selection) {
  var range = document.body.createTextRange();
  range.moveToElementText(document.getElementById(element));
  range.select();
 } else if (window.getSelection) {
  var range = document.createRange();
  range.selectNode(document.getElementById(element));
  window.getSelection().addRange(range);
 }
}

to select the text in the element from the parameter called element.

To make sure you can select your table you have to put it in a <div>:

<div id="foo">
 <table>
  ...
 </table>
</div>

You have to do the same thing with any other thing

<div id="bar">
 <p>
  ...
 </p>
</div>

You can then select that table with this button:

<input type="button" onclick="SelectText(foo)" value="Select table">

And for the other divs:

<input type="button" onclick="SelectText(bar)" value="Select p">

Let me know if it works
Happy coding :) -Charlie

Community
  • 1
  • 1
Charlie
  • 978
  • 1
  • 7
  • 27
  • Thank you for your post I have added the updated code not sue why it is not working [codepen](http://codepen.io/anon/pen/pvoxYQ?editors=101) – bob smitha Nov 16 '14 at 16:29
  • Bob, I tried it in a local file under Firefox 33/Ubuntu 14.04 and that works, the CodePen didn't, however. I don't know what is causing this problem – Charlie Nov 16 '14 at 16:31
  • @bobsmitha I found a working JavaScript on another topic on this site. I checked and this works in both Firefox and Chrome. Let me know if this works for you ;) Here's a CodePen-Snippet: http://codepen.io/anon/pen/gbOQpo – Charlie Nov 16 '14 at 17:23