Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modify user selection on click event on an <a> element?

When I click on the text in the div element i am able to modify the selection. However this is not possible with a link element. Is there a way I can make this work without modifying the html?

document.addEventListener('click', click, true)

function click(e) {
  e.preventDefault()
}

setInterval(() => {
  let selection = window.getSelection();
  selection.modify('extend', 'forward', "documentboundary");
}, 100);
<div>text in div tag</div>
<a href="">text in a tag</a>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does this work?

document.addEventListener('click', click, true)

function click(e) {
  e.preventDefault()
}

setInterval(() => {
  let selection = window.getSelection();
  selection.modify('extend', 'forward', "documentboundary");
}, 100);
a {
  user-select: text;
}
<div>text in div tag</div>
<a href="">text in a tag</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading