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

Time:06-02

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>

CodePudding user response:

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>

  • Related