Jump to content

Recommended Posts

Hi guys,
1.
I am having a problem with setting focus in a specific place, I am using "focus()" how can I control where it will make the focus? I mean after how many chars..
2.
I have a textarea and a button, when I press the buttun the value in textarea is textarea value + "A".
Now, it adds "A" only in the end, what I want is that when I will press the button it will add "A" to the place where the cursor is.
For example:
the value in text area is "Cool" and the cursor is here "Co|ol"[| means cursor] the value after I press the button will be "CoolA", and I want it to be "CoAol".
Thanks.
Ok, for question 1 I found a half solution..works onliy in IE, how can I make it work in FF as well?
this is the full code:
[code]
<script LANGUAGE="Javascript">
var globalCursorPos; // global variabe to keep track of where the cursor was

//sets the global variable to keep track of the cursor position
function setCursorPos() {
  globalCursorPos = getCursorPos(FormPost.Post);
}

//This function returns the index of the cursor location in
//the value of the input text element
//It is important to make sure that the sWeirdString variable contains
//a set of characters that will not be encountered normally in your
//text
function getCursorPos(textElement) {
  //save off the current value to restore it later,
  var sOldText = textElement.value;

//create a range object and save off it's text
  var objRange = document.selection.createRange();
  var sOldRange = objRange.text;

//set this string to a small string that will not normally be encountered
  var sWeirdString = '#%~';

//insert the weirdstring where the cursor is at
  objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

//save off the new string with the weirdstring in it
  var sNewText = textElement.value;

//set the actual text value back to how it was
  objRange.text = sOldRange;

//look through the new string we saved off and find the location of
//the weirdstring that was inserted and return that value
  for (i=0; i <= sNewText.length; i++) {
    var sTemp = sNewText.substring(i, i + sWeirdString.length);
    if (sTemp == sWeirdString) {
      var cursorPos = (i - sOldRange.length);
      return cursorPos;
    }
  }
}

//this function inserts the input string into the textarea
//where the cursor was at
function insertString(stringToInsert) {
  var firstPart = FormPost.Post.value.substring(0, globalCursorPos);
  var secondPart = FormPost.Post.value.substring(globalCursorPos, FormPost.Post.value.length);
  FormPost.Post.value = firstPart + stringToInsert + secondPart;
}
</SCRIPT>
[/code]
works only in IE =\

and for question 2, no this is not what I am looking for, I am looking for something that will put the cursor and focus textarea, let me show you an example:
textarea value is "Cool"
I want to put the cursor and put focus here "C|ool"
| means cursor, how do I do it?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.