S
S
Shimpanze2018-02-20 03:20:40
JavaScript
Shimpanze, 2018-02-20 03:20:40

How to call alert () when the selection (slection) disappears from the text?

Hello!
Can you please tell me how to trigger an event, in this case any alert(), when the selection disappears from the text?
It is this event that is needed, not click or something else.
I will be grateful for help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-02-20
@Shimpanze

It is possible like this:

var prevSelection;

document.addEventListener('mouseup', function() {
  var selection;
  
  if (window.getSelection) {
    selection = window.getSelection().toString();
  } else if (document.selection) {
    selection = document.selection.createRange().text;
  }
  
  if (!selection && prevSelection.length) {
    alert('Text uselected');
  }
  
  prevSelection = selection;
});

Demo .

D
Dima Polos, 2018-02-20
@dimovich85

I won’t tell you much, but I dug up this information, it can give you ideas: selectiononchange event , and select event . I wish you success in your decision.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question