-1

I'm trying to use jQuery to select and copy the text inside the h2 element when i double click the h2 element. (this is with no jQuery involved at all, just the default selecting in browers)

The issue i'm running into is that when i double click it selects less then what i want it to select. when i click 3 times it just selects the whole page.

This is my first post so I don't really know what code I should provide. And I don't have any jQuery written for this yet.

html:

<body>
  <h1>Owl PvP</h1>
  <h2>play.owlpvp.net</h2> <!--This is the part i want to be selected-->
  <figure>
    <img id="mute" src="images/audio-off.svg">
  </figure>

  <nav>
    <ul>
      <li><a href="http://webshop.owlpvp.net">shop</a>
      </li>
      <li><a href="http://forums.owlpvp.net">forums</a>
      </li>
      <li><a href="/staff">staff</a>
      </li>
      <li><a href="#serverstatus">Server</a>
      </li>
    </ul>
  </nav>
  <div id="overlay">
  </div>
</body>

This is what happens when click 3 times, it just selects "owlpvp" when i double click.

website where I want it to work on

Mosh Feu
  • 24,625
  • 12
  • 74
  • 111
DarkEyeDragon
  • 189
  • 1
  • 13

1 Answers1

0

function selectElementContents(el) {
    if (window.getSelection && document.createRange) {
        var sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.selection && document.body.createTextRange) {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.select();
    }
}
<h1 onclick="selectElementContents(this)">Owl PvP</h1>
<h2 onclick="selectElementContents(this)">play.owlpvp.net</h2>
Mosh Feu
  • 24,625
  • 12
  • 74
  • 111