Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!