Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

68
Visualizações
Using getSelection only on a specific element instead of the whole Window/Document?

I am creating a sort of custom bookmarks feature which inserts the highlighted text into an ACF textarea field on a form once the modal button is clicked. This works, but the problem is the function seems to look in the whole document/window and is still running after, so clicking anywhere else gets rid of the inserted text. (Or if I highlight something else within the modal for example it pastes that instead). Please check this short 15 secs video to see what I mean:

https://www.youtube.com/watch?v=uAVQoTuMsCA

And this is the code I'm using:

https://snippi.com/s/h3h281w

I have that chapter variable at the top and I tried replacing window/document with chapter but I just get the following error in the console: Uncaught TypeError: Cannot read properties of null (reading 'getSelection') at HTMLDivElement.getSelectedText

Does anyone know how I can get this to only work on the area where the highlighted text is (#chapter) and not have it still happening after the text is inserted?

It was suggested to me to remove the event listener when the modal is open but I don't think I'm doing that right or the function itself is wrong.

Appreciate any help anyone can provide. Thanks.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda