Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Right. The short answer is: <em>you can't</em>.</p> <p>That, however, isn't really very helpful. So, if you're prepared to accept a slightly hacky approach, with at least one caveat, you can:</p> <p>Given the html:</p> <pre><code>&lt;div id="wrap"&gt; &lt;span class="copyText"&gt;This is some text to copy.&lt;/span&gt; &lt;span&gt;Can't copy &lt;em&gt;this&lt;/em&gt; (automatically...)!&lt;/span&gt; &lt;span class="copyText"&gt;And this is yet more text.&lt;/span&gt; &lt;/div&gt; </code></pre> <p>And the CSS:</p> <pre><code>span.copyText { position: relative; display: block; } textarea { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0 none transparent; margin: 0; padding: 0; outline: none; resize: none; overflow: hidden; font-family: inherit; font-size: 1em; } </code></pre> <p>You can use the following jQuery:</p> <pre><code>$(document).ready( function() { $('.copyText').click( function() { if ($('#tmp').length) { $('#tmp').remove(); } var clickText = $(this).text(); $('&lt;textarea id="tmp" /&gt;') .appendTo($(this)) .val(clickText) .focus() .select(); return false; }); $(':not(.copyText)').click( function(){ $('#tmp').remove(); }); }); </code></pre> <p>With the requisite <a href="http://jsfiddle.net/davidThomas/ZmYBh/" rel="noreferrer">JS Fiddle demo, at: http://jsfiddle.net/davidThomas/ZmYBh/</a>.</p> <p>The main caveat is that the element you want to copy <em>cannot</em> (with this approach at least) wrap from one line to the next (<em>unless</em> it's also <code>display: block</code>), I suspect it has something to do with how native <code>form</code> elements are rendered as 'solid' rectangles, unlike most other <code>inline</code> elements, such as html which form a more...'fluid'(?) rectangle when wrapping).</p> <p>There may be others, however.</p> <p><a href="http://jsfiddle.net/davidThomas/ZmYBh/1/" rel="noreferrer">JS Fiddle demo to show that it does work with wrapping text, so long as the parent container element (<code>span</code>) is still <code>display: block;</code></a>.</p> <p><hr /> <strong>Edited:</strong> to add that I tried using <code>input</code>s instead of <code>textarea</code> which, predictably, failed to work any differently/better than <code>textarea</code>, and also <code>&lt;span contenteditable&gt;</code>, which (again, predictably) didn't select the text at all, but did insert the caret at the beginning of the text.</p> <p>Sigh. I think there should be a far easier answer to this question, but I can't see it for the life of me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload