Created
May 31, 2012 00:11
-
-
Save omniosi/2839720 to your computer and use it in GitHub Desktop.
Capitalize input text value
This file contains hidden or 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
/*Add to your script:*/ | |
jQuery.fn.capitalize = function() { | |
$(this[0]).keyup(function(event) { | |
var box = event.target; | |
var txt = $(this).val(); | |
var start = box.selectionStart; | |
var end = box.selectionEnd; | |
$(this).val(txt.replace(/^(.)|(\s|\-)(.)/g, function($1) { | |
return $1.toUpperCase(); | |
})); | |
box.setSelectionRange(start, end); | |
}); | |
return this; | |
} | |
/*Then just attach capitalize() to any selector:*/ | |
$('#myform input').capitalize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment