How can I prevent text selection in a textarea using only JavaScript?

  Kiến thức lập trình

I’m working on a webpage where I need to prevent text selection within a textarea element.

I tried to use the selectionchange event to stop the selection, but it doesn’t work:

const textarea = document.getElementById("myTextarea");

document.addEventListener("selectionchange", (e) => {
   e.stopPropagation();
   e.stopImmediatePropagation();
   e.preventDefault();
   console.log();
});
<textarea>This text is somewhat long, but not so looooooonnnng</textarea>

LEAVE A COMMENT