Highlighting text with class

by mamounothman

HTML

<div>
  <span class='highlighted'>Selecting this text should make this message appear if text that already contains class is highlighted</span>. More random text.
</div>

CSS

.highlighted {
  background-color: lightGrey;
  user-select: none;
}

JavaScript

document.addEventListener('selectionchange', () => {
  let selection = window.getSelection();
  console.log(selection.toString());
});