0

I have a JQuery function to Capitalize the first alphabet of each word of a string. Which is working fine.

The issue I am getting is, that, when I type any alphabet at the beginning of the word the cursor moves to the last alphabet of the string. If I type anything in between the cursor stays at it's place.

I searched and got some solutions but that where .jsp or javascript based. Looking for some simple JQuery solution.

function pCase(strg) {
  var str = strg;
  str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
  });
  return str;
}


function propCase(obj) {
  obj.value = pCase(obj.value);
}

$(function() {
  $(".properCase").on("keyup", function() {
    propCase(this);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner-message">
  <input type="text" class="properCase" />
</div>
abbas
  • 238
  • 2
  • 18
  • @T.J. I am not looking for a classic `javascript` solution. Looking for JQuery solution. – abbas Jun 13 '18 at 13:26
  • Look at the answers there, and you'll find answers using jQuery for part of it. But note that jQuery doesn't have anything that will do the actual handling of the cursor position within the input, though; that part you do directly with the DOM (not JavaScript -- it's JavaScript whether you're using jQuery or not). – T.J. Crowder Jun 13 '18 at 13:34
  • I have resolved you problem. Kindly, ask a new question i will post my answer over there. – gkbstar Jun 14 '18 at 05:55
  • Anyways i have updated your jsfiddle code. – gkbstar Jun 14 '18 at 06:01
  • @gkbstar can you share the link of jsfiddle, as I can see the same issue https://jsfiddle.net/xpvt214o/222343/ here. – abbas Jun 14 '18 at 08:10
  • @abbas check @ https://jsfiddle.net/gkbstar/quyrhxmc/8/ – gkbstar Jun 20 '18 at 10:32
  • @gkbstar Thanks this is not moving the cursor at the end but while typing it is also not making the first alphabet capital. – abbas Jun 20 '18 at 10:45

0 Answers0