Skip to content

Instantly share code, notes, and snippets.

@omniosi
Created May 31, 2012 00:11
Show Gist options
  • Save omniosi/2839720 to your computer and use it in GitHub Desktop.
Save omniosi/2839720 to your computer and use it in GitHub Desktop.
Capitalize input text value
/*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