Advertisement

11.27.2008 at 09:45AM PST, ID: 23940793 | Points: 250
[x]
Attachment Details

Go To Caret Position

Tags:

Mozilla, JavaScript, FireFox 3.x

Hello,

I have a JavaScript function that sets the position of the caret in a <TextArea> form control ( setPosition() ).  The problem is that if the position is set below the scrollable area, the position is set correctly, but focus does not move to it so as it is viewable.

I need this to work in FireFox 3.x

What can you recommend?

Thanks,

Michael

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
function setPosition() {
      var sSelectedValue = document.forms['form1'].elements['topics'].value;
      var sTextAreaValue = document.form1.note.value;
      var iPosition      = sTextAreaValue.indexOf(sSelectedValue);
      //alert('iPosition: ' + iPosition);
      setCaretPosition('idTextArea', iPosition);
   }
 
   function setCaretPosition(elemId, caretPos) {
       var elem = document.getElementById(elemId);
 
       if(elem != null) {
           if(elem.createTextRange) {
               var range = elem.createTextRange();
               range.move('character', caretPos);
               range.select();
           }
           else {
               if(elem.selectionStart) {
                   elem.focus();
                   elem.setSelectionRange(caretPos, caretPos);
               } 
               else
                   elem.focus();
           }
       }
   }
 
   function setSelRange(inputEl, selStart, selEnd) { 
      if (inputEl.setSelectionRange) { 
         inputEl.focus(); 
         inputEl.setSelectionRange(selStart, selEnd); 
      } else if (inputEl.createTextRange) { 
         var range = inputEl.createTextRange(); 
         range.collapse(true); 
         range.moveEnd('character', selEnd); 
         range.moveStart('character', selStart); 
         range.select(); 
      } 
   }
Top Expert: mplungjan
Expert Since: 04/06/1998
Accepted Solutions: 7077
Computer Expertise: Advanced
mplungjan has been an Expert for 10 years 9 months, during which he has posted 43816 comments and answered 7077 questions. mplungjan is just one of 1118 experts in the Firefox Web Browser Zone.
 
 
20081119-EE-VQP-48 - Hierarchy / EE_QW_2_20070628