user695868
user695868

Reputation:

Capitalize first letter in textbox

I am using the following code to capitalize the first letter entered into a text box. This works fine at first, but if the entire box is deleted, after typing again it will not capitalize again.

Can this be fixed?

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

jsfiddle: http://jsfiddle.net/VBXbz/

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

Upvotes: 1

Views: 1799

Answers (2)

Jai
Jai

Reputation: 74738

.one makes the event to be happen just once in the page use .on instead:

$('.userBox').on('DOMAttrModified ....

updates:

you can try something like this:

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

Upvotes: 2

danronmoon
danronmoon

Reputation: 3873

You want on, not one:

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

one is for handlers you want executed at most once

Upvotes: 5

Related Questions