44 lines
1.0 KiB
JavaScript
44 lines
1.0 KiB
JavaScript
|
function select(element) {
|
||
|
var selectedText;
|
||
|
|
||
|
if (element.nodeName === 'SELECT') {
|
||
|
element.focus();
|
||
|
|
||
|
selectedText = element.value;
|
||
|
}
|
||
|
else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
|
||
|
var isReadOnly = element.hasAttribute('readonly');
|
||
|
|
||
|
if (!isReadOnly) {
|
||
|
element.setAttribute('readonly', '');
|
||
|
}
|
||
|
|
||
|
element.select();
|
||
|
element.setSelectionRange(0, element.value.length);
|
||
|
|
||
|
if (!isReadOnly) {
|
||
|
element.removeAttribute('readonly');
|
||
|
}
|
||
|
|
||
|
selectedText = element.value;
|
||
|
}
|
||
|
else {
|
||
|
if (element.hasAttribute('contenteditable')) {
|
||
|
element.focus();
|
||
|
}
|
||
|
|
||
|
var selection = window.getSelection();
|
||
|
var range = document.createRange();
|
||
|
|
||
|
range.selectNodeContents(element);
|
||
|
selection.removeAllRanges();
|
||
|
selection.addRange(range);
|
||
|
|
||
|
selectedText = selection.toString();
|
||
|
}
|
||
|
|
||
|
return selectedText;
|
||
|
}
|
||
|
|
||
|
module.exports = select;
|