Capitalize the alphabet if there is a space before entering text in the text box

I want to make the ALPHABET title the user entered in the text box if there is a space before it.

The user example writes "test new", so "n" should be capital, and it will be so smooth that it seems like the user has pressed the shift key

I think we can do the same in the keydown (jquery) event

The code is what I tried:

$('.name').live("keydown", function (e) { try { if ($('.name').val().length > 1) { if ($('.name').val().substring($('.name').val().length - 1) == " ") { // HERE can we do something like e.shift key etc to get desired result } } } catch (err) { alert(err); } }); 
+4
source share
3 answers

Alternative

 ​.name { text-transform:capitalize; } 
+6
source

Try:

 $('#foo').on('keyup', function () { $(this).val(function (i, val) { return val.replace(/(\s)(\S)/g, function ($0, $1, $2) { return $1 + $2.toUpperCase(); }); }); });​ 

demo: http://jsfiddle.net/qPSpK/2/

+5
source

I am surprised at the cleanliness of @AlexK's answer, but if you really need the first word in lowercase (or just require a different form of seamless control over the text), try this jQuery:

 $(function(){ $("textarea").bind("keypress", function(e){ var cap = ""; if(e.which >= 97 && e.which <= 122) { cap = String.fromCharCode(e.which).toUpperCase(); } if( cap != "" ) { var text = $(this).val(); if( text[text.length-1] == " ") { $(this).val(text+cap); return false; } } }); });​ 

http://jsfiddle.net/dkyYr/1/

Edit: changed the switch statement to character mapping

+2
source

Source: https://habr.com/ru/post/1401397/


All Articles