Skip to content

Instantly share code, notes, and snippets.

@refo
Last active August 3, 2017 13:17
Show Gist options
  • Save refo/f33de89471d9292885a52313c07a7e39 to your computer and use it in GitHub Desktop.
Save refo/f33de89471d9292885a52313c07a7e39 to your computer and use it in GitHub Desktop.
Javascript, select all on click
<!-- Example Usage -->
<div class="form-group col-sm-6">
<label class="control-label">Link <sup class="alert-danger">*</sup></label>
<div ng-bind="item.url" onclick="selectAll(this);" class="form-control"></div>
</div>
// Select all text node of an element
function selectAll(element) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(element);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
@refo
Copy link
Author

refo commented Nov 17, 2016

@refo
Copy link
Author

refo commented Aug 3, 2017

Updated according to this and the source:
https://www.chromestatus.com/features/6680566019653632

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