Skip to content

Instantly share code, notes, and snippets.

@LeeFlannery
Created August 19, 2015 17:33
Show Gist options
  • Save LeeFlannery/c06b26858e979bc4e4bf to your computer and use it in GitHub Desktop.
Save LeeFlannery/c06b26858e979bc4e4bf to your computer and use it in GitHub Desktop.
jQuery - select all text on focus
function selectOnFocus(input) {
input.each(function (index, elem) {
var $elem = $(elem);
var ignoreNextMouseUp = false;
$elem.mousedown(function () {
if (document.activeElement !== elem) {
ignoreNextMouseUp = true;
}
});
$elem.mouseup(function (ev) {
if (ignoreNextMouseUp) {
ev.preventDefault();
ignoreNextMouseUp = false;
}
});
$elem.focus(function () {
$elem.select();
});
});
}
selectOnFocus($("input:text"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment