user695868
user695868

Reputation:

Issue with capitalize first letter of textbox

I am using the following code to capitalize the first letter entered into a textbox. Problem is when you tab to another textbox, then tab back and enter something, it will not erase what is there .. it appends what you type.

Can this be fixed?

$(function () {
    $('.userBox').on('DOMAttrModified textInput input keypress paste focus', 
      function (e) {
         $(this).val($(this).val().slice(0, 1).toUpperCase() + 
                $(this).val().slice(1));
      });
});

Demo: http://jsfiddle.net/VBXbz/8/

Upvotes: 0

Views: 298

Answers (2)

Sarath
Sarath

Reputation: 11

<script type="text/javascript" language="javascript">
    function Capitalize(id) {
        var txt = document.getElementById(id);
        txt.value = txt.value.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
    }
</script>

Sarath@f1

Upvotes: 1

JHollanti
JHollanti

Reputation: 2413

I would just bind focus to another function that will clear the contents of the input field. Like so:

$('.userBox').on('focus', function (e) {
    $(this).val("");
}); 

Then you'd essentially have http://jsfiddle.net/KWRef/.

Unless i missed some point you wanted to make?

Upvotes: 0

Related Questions