-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.selectText.js
42 lines (40 loc) · 1.07 KB
/
jquery.selectText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*global jQuery*/
(function ($) {
"use strict";
$.fn.selectText = function () {
var methods = {
deselect_all_text_in_document: function () {
if (document.selection) {
document.selection.empty();
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
}
},
select_text_by_id: function (element) {
var range;
if (document.selection) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
range = document.createRange();
range.selectNode(element);
window.getSelection().addRange(range);
}
return range
}
};
methods.deselect_all_text_in_document();
this.each(function () {
return methods.select_text_by_id(this);
});
};
$('.selectText').on('click', function (event) {
event.preventDefault();
var targetElementSelector = $(this).attr('href');
if (!targetElementSelector) {
targetElementSelector = $(this).data('targetSelector');
}
$(targetElementSelector).selectText();
});
}(jQuery));