Created
August 19, 2015 17:33
-
-
Save LeeFlannery/c06b26858e979bc4e4bf to your computer and use it in GitHub Desktop.
jQuery - select all text on focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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