Skip to content

Instantly share code, notes, and snippets.

@AlexMocioi
Created April 10, 2014 06:33
Show Gist options
  • Save AlexMocioi/10348145 to your computer and use it in GitHub Desktop.
Save AlexMocioi/10348145 to your computer and use it in GitHub Desktop.
Funcție ajutătoare să faci ceva cu un text selectat prin dublu-click într-o fereastră HTML
function getSelectedText() {
if (window.getSelection) return window.getSelection().toString();
if (document.getSelection) return document.getSelection().toString();
if (document.selection) return document.selection.createRange().text.toString();
return "";
}
//În altă parte vrei să se declanșeze ceva când utilizatorul a selectat un cuvânt:
//- merge pe selecție prin drag peste text, când termini, pe mouseUp
//- merge când faci dublu-click, că face și selecția și apoi la mouseUp declanșează acțiunea
// pun acum evenimentul pe mouseUp și dacă am selecție de cuvânt, caut cabinetul după acel cuvânt
$('#tabelaIncasariParserate td').mouseup(function(evt) {
// Dacă nu-i în TD, abandonez
if ( evt.target.tagName!="TD" ) return;
var crtTR = $(evt.target).closest("tr")[0];
var cuvant = trim(getSelectedText());
if (cuvant=="") return;
// de aici înseamnă că am text selectat, fac ceva cu el, lookup, display ...
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment