AUTO SELECTING HTML TEXT WITH JAVASCRIPT
Here is a function that when called on a particular HTML element will auto-select all of its inner text. The function works on elements such as INPUT (text), TEXTAREA, DIV, SPAN, TD, and PRE. It is also cross browser compatible.
var autoSelect = function (el) {
if (/textarea/i.test(el.tagName) || (/input/i.test(el.tagName) && /text/i.test(el.type))) {
el.select();
} else if (!!window.getSelection) { // FF, Safari, Chrome, Opera
var sel = window.getSelection();
var range = document.createRange();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (!!document.selection) { // IE
document.selection.empty();
var range = document.body.createTextRange();
range.moveToElementText(el);
range.select();
}
};
Demo:
Click on this text to auto select it.