0

I'm using the following to prevent text selection when a label is double-clicked:

label.addEventListener('mousedown', (event) => {
  if (!event.defaultPrevented && event.detail > 1) {
    event.preventDefault();
  }
})

The code works as intended, and text is not selected when the label is double-clicked. However, I have noticed that in Firefox, if I check for window.getSelection() after double-clicking the label, the label text is still selected within JavaScript (this isn't the case in Chrome).

When I remove the conditional statement that checks for !event.defaultPrevented && event.detail > 1, the window.getSelection() call no longer returns the label's text as selected.

It seems very strange that even though the condition is passed, the behaviour between the 2 is different.

I have created a fiddle to demonstrate the issue: link.

My question is: Is this a bug or intended behaviour in Firefox? And are there any workarounds for this issue?

  • https://stackoverflow.com/questions/826782/how-to-disable-text-selection-highlighting – epascarello May 09 '23 at 15:16
  • Does this answer your question? [How to disable text selection highlighting](https://stackoverflow.com/questions/826782/how-to-disable-text-selection-highlighting) – nameless May 09 '23 at 16:04
  • Thanks for the responses. I don't want to prevent text selection per se, but only prevent on double-click. – code_sanchu May 09 '23 at 16:10

0 Answers0