Uppercase first letter of variable

后端 未结 23 1463
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  伪装坚强ぢ
    2020-11-30 21:20

    The string to lower before Capitalizing the first letter.

    (Both use Jquery syntax)

    function CapitaliseFirstLetter(elementId) {
        var txt = $("#" + elementId).val().toLowerCase();
        $("#" + elementId).val(txt.replace(/^(.)|\s(.)/g, function($1) {
        return $1.toUpperCase(); }));
        }
    

    In addition a function to Capitalise the WHOLE string:

    function CapitaliseAllText(elementId) {
         var txt = $("#" + elementId).val();
         $("#" + elementId).val(txt.toUpperCase());
         }
    

    Syntax to use on a textbox's click event:

    onClick="CapitaliseFirstLetter('TextId'); return false"
    

提交回复
热议问题