Skip to content

Instantly share code, notes, and snippets.

@iacchus
Created August 19, 2021 23:19
Show Gist options
  • Save iacchus/a8049a9539303626fac1074c24c64d3c to your computer and use it in GitHub Desktop.
Save iacchus/a8049a9539303626fac1074c24c64d3c to your computer and use it in GitHub Desktop.
// see it here:
// https://stackoverflow.com/questions/7563169/detect-which-word-has-been-clicked-on-within-a-text
// https://jsbin.com/oXetAcI/4/edit?html,js,output
$.fn.OneClickSelect = function(){
return $(this).on('click',function(){
var range, selection;
if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
};
// Apply to these elements
$('p, #all-select').OneClickSelect();
@iacchus
Copy link
Author

iacchus commented Aug 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment