Home > other >  textarea trailing new lines when selected via keyboard vs mouse
textarea trailing new lines when selected via keyboard vs mouse

Time:01-20

If I initialize a textarea with 3 rows of text and 3 newlines like this

<textarea rows="10" cols="50">
A
B
C



</textarea>

When you highlight the text with a mouse the clipboard will contain all 3 new lines.
Mouse Highlighting

When you highlight the text with ctrl a and copy the clipboard will contain an extra 4th new line.
Keyboard Highlighting

This is causing me some problems in a WYSIWYG editor that I'm working on. How would you make the copy paste behavior act the same no matter if the text was highlighted with a keyboard or a mouse

CodePudding user response:

Ok. But where is problem? In both cases you have the same content in clipboard.

Open console and try this:

function showCB() {
  let text = window.getSelection();
  text = text.toString();
  console.log(text.length);
}
document.addEventListener('mouseup', showCB);
document.addEventListener('keyup', showCB);
document.addEventListener('selectionchange', showCB);
<textarea rows="10" cols="50">
A
B
C



</textarea>

  • Related