Skip to content

Instantly share code, notes, and snippets.

@andreyshr
Last active September 21, 2018 09:48
Show Gist options
  • Save andreyshr/ffc2e03aa3aa6726cc9440da4b168e1e to your computer and use it in GitHub Desktop.
Save andreyshr/ffc2e03aa3aa6726cc9440da4b168e1e to your computer and use it in GitHub Desktop.
getSelectedText()
// функция для получение выделенного текста
function getSelectedText(){
var text = "";
if (window.getSelection) {
text = window.getSelection();
}else if (document.getSelection) {
text = document.getSelection();
}else if (document.selection) {
text = document.selection.createRange().text;
}
return text;
}
$(document).ready(function() {
// при нажатии на Ctrl + Enter
var isCtrl = false;
$(document).keyup(function (e) {
if(e.which == 17) isCtrl = false;
}).keydown(function (e) {
if(e.which == 17) isCtrl=true;
if(e.which == 13 && isCtrl == true) {
// получаем и показываем выделенный текст
alert(getSelectedText());
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment