-2

I have a function with onkeyup to modify the letters of text typed in uppercase.

When I tried to edit a letter in the middle of the input text the cursor will automatically position at the end of text.

I would like the cursor to keep the same position.

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeyup

Sarra
  • 11
  • 5
  • 3
    Possible duplicate of [Stop cursor from jumping to end of input field in javascript replace](http://stackoverflow.com/questions/8219639/stop-cursor-from-jumping-to-end-of-input-field-in-javascript-replace) – Blue Apr 28 '17 at 10:34

4 Answers4

0

Try This:

 <input type="text" id="fname" onkeypress="myFunction()">
 <script>
     function myFunction() {
       var x = document.getElementById("fname");
       x.value = x.value.toUpperCase();    
     }
 </script>
0

I don't know why you need such thing.But just try this, it would help.

#fname{    
   text-transform: uppercase;
}

This is using simple CSS. If need uppercase value at the end you can try this after using that CSS.

<input type="text" id="fname" onfocusout="myFunction()">

<script>
function myFunction() {
    var x = document.getElementById("fname");
    x.value = x.value.toUpperCase();
}
</script>
Kranthi Samala
  • 171
  • 1
  • 6
0

To uppercase first letter only:

function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

To UpperCase all the letters

this.value.toUpperCase();
User6667769
  • 745
  • 1
  • 7
  • 25
-1
$("#fname").on('input', function(){
     var start = this.selectionStart,
     end = this.selectionEnd;
     this.value = this.value.toUpperCase();
     this.setSelectionRange(start, end);
});

If you want to make only first latter to upperCase and all other to Lower case. You can make it like.

#fname {
    text-transform: lowercase;
}
#fname:first-letter {
   text-transform: uppercase;
}

But Please use only class for make some styles.

NoName
  • 183
  • 10
  • Thank you very much. It works well. Can I modify the first letter of text in uppercase and the rest in lower case? – Sarra Apr 28 '17 at 11:24
  • You are welcome. It would be greate if you give me a like and check my answer like correct. – NoName Apr 28 '17 at 12:26