Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
THIS IS MY JAVASCRIPT
XML
<script language="javascript" type="text/javascript">
       function ToUpper(id) {
           document.getElementById(id).value = document.getElementById(id).value.toUpperCase();
       }
   </script>

my problem is i enter total name and it will change to uppercase
if i enter another text in middle of name the letter is showed from last
example

first entered name
abc xyz
SQL
after i want to change name in mddle like bellow
abc defxyz
<pre lang="sql">if enter any two letters after one letter the blinking cursor go to last of name and loke bellow
abc d xyzef



how to solve can you tell me
Posted
Updated 27-Apr-15 17:28pm
v4

1 solution

The issue is that editing the text inputs value as someone types will reset the caret (cursor) position to after the new text.

You need to A: Get where the caret is before changing the text and B: Set the caret after changing the text:

JavaScript
function A(element){
 //You will won't to add some error checking here - such as if(!element.selection) return 0;
 return element.selectionStart;
}

function B(element, caret){
 //Selecting a range with the same start and end will place the caret at that point
 element.setSelectionRange(caret,caret);
}

//just pass the element instead of id
function toUpper(element){
  var caret = A(this);
  this.value = this.value.ToUpperCase();
  B(this,caret);
}


Oh and pass the whole element (you don't HAVE to though):
ASP.NET
<asp:textbox id="txtPatientname" runat="server" onkeyup="ToUpper(this)" cssclass="txtbox" font-names="Calibri" font-size="Medium" height="20px" maxlength="50" tabindex="6" tooltip="Patient name" width="175px" xmlns:asp="#unknown"></asp:textbox>


Hope that helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900