0
$(document).ready(function () {
    $(document).on('keydown', '.FIRSTNAME',
        function (event) {
            if (this.selectionStart == 0 && event.keyCode >= 65 && event.keyCode <= 90 && !(event.shiftKey) && !(event.ctrlKey) && !(event.metaKey) && !(event.altKey)) {
                var $t = $(this);
                event.preventDefault();
                var char = String.fromCharCode(event.keyCode);
                $t.val(char + $t.val().slice(this.selectionEnd));
                this.setSelectionRange(start, end);
            }
        });
});

can you use my code and edit it?

the difference is the first letter in every word...

I just want to capitalize the first letter in every word

someone help me... tnx in advance

enter image description here

J.A.Manato
  • 27
  • 1
  • 6

5 Answers5

4

No need of using the javascript. You can simply do it in CSS using text-transform: capitalize; like this

<input type='text' name='name' class='name' style="text-transform: capitalize;" placeholder='Enter your name here'/>
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31
2

jquery this way easy capitalize first letter of EACH WORD, in input type text

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<div><label>Capitalize the first letter of all words in a string: </label><input type="text" id="txt_firstCapital" name="txt_firstCapital" /></div>

<script>

jQuery(document).ready(function() {
 
 jQuery('#txt_firstCapital').keyup(function() 
 {
  var str = jQuery('#txt_firstCapital').val();
       
  
  var spart = str.split(" ");
  for ( var i = 0; i < spart.length; i++ )
  {
   var j = spart[i].charAt(0).toUpperCase();
   spart[i] = j + spart[i].substr(1);
  }
      jQuery('#txt_firstCapital').val(spart.join(" "));
 
 });
});
</script>
1

function capitalize(obj)
{
    obj.value = obj.value.split(' ').map(eachWord=>
      eachWord.charAt(0).toUpperCase() + eachWord.slice(1)
    ).join(' ');
}
<input type='text' id='textfield' onkeyup='capitalize(this)'>
Dhana Sekar
  • 631
  • 1
  • 4
  • 13
1

This can be done with this function in JS

function firstCap(str)
{
 return str.toString().replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + 
 txt.substr(1).toLowerCase();});
}
indago
  • 2,041
  • 3
  • 29
  • 48
-1

You only needed css to fix it. Add the below code in your css.

.FIRSTNAME{
  text-transform: capitalize;
}

OR try this

<input type="text" id="txtval" style="text-transform:capitalize" />

this works

NB: There is no need of jquery for this.

Veena K. Suresh
  • 942
  • 5
  • 16