Capitalize First Letter of Every Word on Keypress


/ Published in: jQuery
Save to your folder(s)

Prevents users from entering all caps for titles, text.


Copy this code and paste it in your HTML
  1. //usage
  2. $("input").keyup(function() {
  3. toUpper(this);
  4. });
  5.  
  6.  
  7. //function
  8. function toUpper(obj) {
  9. var mystring = obj.value;
  10. var sp = mystring.split(' ');
  11. var wl=0;
  12. var f ,r;
  13. var word = new Array();
  14. for (i = 0 ; i < sp.length ; i ++ ) {
  15. f = sp[i].substring(0,1).toUpperCase();
  16. r = sp[i].substring(1).toLowerCase();
  17. word[i] = f+r;
  18. }
  19. newstring = word.join(' ');
  20. obj.value = newstring;
  21. return true;
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.