How to prevent part of HTML text from being copied when copying adjacent?

Matteo Ferla

I wrote a JavaScript widget which adds line numbers and one thing I cannot figure out is preventing the numbers from being copied when one copies the text. I want people to be able to copy the sequences around the numbers, but not the numbers. Here is an example of the script's results.

Basically:

<span>useful stuff to be copied </span>
<span style="some-mysterious-setting: True;"> gloss to be discarded in selection </span>
<span> useful stuff to be copied</span>

The numbers are implemented as a separate span elements and not as a table or anything that fancy. I tried user-select: none; and its variants in CSS, but that means it does not get highlighted, but it copies nevertheless the numbering.

Alejalapeno

So you don't need Javascript for this.

The solution is to use a pseudo element and not actually put the number in your element.

<span class="line-number" data-line-number="1"></span>

CSS:

.line-number::before {
  content: attr(data-line-number);
}

td:nth-of-type(1) {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
td:nth-of-type(1)::before {
  content: attr(data-line-number);
}
<table>
  <tr>
    <td data-line-number="1"></td>
    <td>Test row</td>
  </tr>
  <tr>
    <td data-line-number="2"></td>
    <td>Test row</td>
  </tr>
  <tr>
    <td data-line-number="3"></td>
    <td>Test row</td>
  </tr>
  <tr>
    <td data-line-number="4"></td>
    <td>Test row</td>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to prevent part of HTML text from being copied when copying adjacent?

From Dev

Prevent session from being replicated when JSESSIONID cookie copied

From Dev

How to see the hidden formats that get copied when copying HTML content?

From Dev

C - Copying text from a file results in unknown characters being copied over as well

From Dev

Prevent line numbers from being copied to clipboard

From Dev

regex - prevent two characters from being adjacent

From Dev

How to prevent text copied from OneNote to be pasted as an image

From Dev

How to prevent Ditto clipboard manager from saving text copied in KeepPassXC?

From Dev

Prevent Word from Including Bullet Letter when Copying Text

From Dev

Prevent war file from being copied to .m2/repository when running mvn clean install

From Dev

How to prevent html div from expanding when text size is changed

From Dev

How to prevent my empty strings from being part of the length

From Dev

HTML Prevent part of text from going to next line

From Dev

How to prevent a text from overflowing AND from being clipped?

From Dev

Can I prevent object from being copied by std::memcpy?

From Dev

Is there a very effective method to prevent a .jpg file for being copied from a page?

From Dev

How to get part of a forumla to increase when copied?

From Dev

How to prevent binary file folder also being copied to output dir?

From Dev

Prevent user from copying text on mobile browsers

From Dev

Prevent auto-selected text from copying

From Dev

how to prevent text inside an input from being overridden

From Dev

How to prevent HTML elements from being pushed down the page

From Dev

How to prevent files from being stolen by editing html code?

From Dev

How to prevent database from being DDOSed by application when cache expires?

From Dev

How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?

From Dev

How to prevent a thread from being terminated when conditions are not satisfied momentarily?

From Dev

How to prevent a <div> element from being resized when zooming

From Dev

How to prevent score from being overwritten when the same activity is called

From Dev

How to prevent a Fragment from being added when an Activity is closing

Related Related

  1. 1

    How to prevent part of HTML text from being copied when copying adjacent?

  2. 2

    Prevent session from being replicated when JSESSIONID cookie copied

  3. 3

    How to see the hidden formats that get copied when copying HTML content?

  4. 4

    C - Copying text from a file results in unknown characters being copied over as well

  5. 5

    Prevent line numbers from being copied to clipboard

  6. 6

    regex - prevent two characters from being adjacent

  7. 7

    How to prevent text copied from OneNote to be pasted as an image

  8. 8

    How to prevent Ditto clipboard manager from saving text copied in KeepPassXC?

  9. 9

    Prevent Word from Including Bullet Letter when Copying Text

  10. 10

    Prevent war file from being copied to .m2/repository when running mvn clean install

  11. 11

    How to prevent html div from expanding when text size is changed

  12. 12

    How to prevent my empty strings from being part of the length

  13. 13

    HTML Prevent part of text from going to next line

  14. 14

    How to prevent a text from overflowing AND from being clipped?

  15. 15

    Can I prevent object from being copied by std::memcpy?

  16. 16

    Is there a very effective method to prevent a .jpg file for being copied from a page?

  17. 17

    How to get part of a forumla to increase when copied?

  18. 18

    How to prevent binary file folder also being copied to output dir?

  19. 19

    Prevent user from copying text on mobile browsers

  20. 20

    Prevent auto-selected text from copying

  21. 21

    how to prevent text inside an input from being overridden

  22. 22

    How to prevent HTML elements from being pushed down the page

  23. 23

    How to prevent files from being stolen by editing html code?

  24. 24

    How to prevent database from being DDOSed by application when cache expires?

  25. 25

    How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?

  26. 26

    How to prevent a thread from being terminated when conditions are not satisfied momentarily?

  27. 27

    How to prevent a <div> element from being resized when zooming

  28. 28

    How to prevent score from being overwritten when the same activity is called

  29. 29

    How to prevent a Fragment from being added when an Activity is closing

HotTag

Archive