The answers listed are partial according to me. I have linked below two examples of how to do this in Angular and with JQuery.
This solution has the following features:
- Works for all browsers that support JQuery, Safari, Chrome, IE, Firefox, etc.
- Works for Phonegap/Cordova: Android and IOs.
- Only selects all once after input gets focus until next blur and then focus
- Multiple inputs can be used and it does not glitch out.
- Angular directive has great re-usage simply add directive select-all-on-click
- JQuery can be modified easily
JQuery: http://plnkr.co/edit/VZ0o2FJQHTmOMfSPRqpH?p=preview
$("input").blur(function() { if ($(this).attr("data-selected-all")) { //Remove atribute to allow select all again on focus $(this).removeAttr("data-selected-all"); } }); $("input").click(function() { if (!$(this).attr("data-selected-all")) { try { $(this).selectionStart = 0; $(this).selectionEnd = $(this).value.length + 1; //add atribute allowing normal selecting post focus $(this).attr("data-selected-all", true); } catch (err) { $(this).select(); //add atribute allowing normal selecting post focus $(this).attr("data-selected-all", true); } } });
Angular: http://plnkr.co/edit/llcyAf?p=preview
var app = angular.module('app', []); //add select-all-on-click to any input to use directive app.directive('selectAllOnClick', [function() { return { restrict: 'A', link: function(scope, element, attrs) { var hasSelectedAll = false; element.on('click', function($event) { if (!hasSelectedAll) { try { //iOS, Safari, thows exception on Chrome etc this.selectionStart = 0; this.selectionEnd = this.value.length + 1; hasSelectedAll = true; } catch (err) { //Non iOS option if not supported, e.g. Chrome this.select(); hasSelectedAll = true; } } }); //On blur reset hasSelectedAll to allow full select element.on('blur', function($event) { hasSelectedAll = false; }); } }; }]);
<label>for the label and not thevalue. You can use JS and CSS to make it look the same, while not being so anti-semantic. dorward.me.uk/tmp/label-work/example.html has an example using jQuery.input { user-select: all; }