FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

How to get keyboard cursor position i a field....

 


adilfaridmardan
Hello...

Friends i am writing a java script code for a field...
I am using onkeydown event. when the user enter "." then what ever he writes or entered the field displayed the text i have already provided and holds the user data in a specific field....

Now the problem lies when the user uses the mouse and move the cursor(Keyboard) to the middle of the text entered... Now it is very hard for me to catch the current position of the keyboard cursor and append (or add) the text the customer entered to that position.....

Hope you understand what i mean.....(after pressing "." key) the user input is actually not displayed to the user, instead a common sentense already hardcodded is displayed....

How to find that cursor position in order to add upcomming text to that location...

By the way My Name is Adil Farid... I am new to Javascript Programming, i have only done form validation with javascript before.... i am from Peshawar (The Land of Pathans)
alalex
So what you mean is that whenever a dot is written in the text input you will insert a custom string after the dot?

In case that is right all you have to do would be something like this:
Code:
<script type="text/javascript" language="javascript">
<!--
function echoText(id){
   var holder = document.getElementById(id);
   var newChar = holder.value[holder.value.length - 1];
   if(newChar == '.'){
      //New character is a dot:
      holder.value = holder.value+' A dot was entered before this';
      return true;
   }else{
      return false;
   }
}
-->
</script>


And a sample text input:
Code:
<input name="text" type="text" id="text" size="50" maxlength="50" onkeydown="echoText(this.id)" />


If this is not what you were expecting tell me and I'll rewrite the function Wink
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.