Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. Javascript: How to get/set caret position in TextArea control?

Javascript: How to get/set caret position in TextArea control?

Scheduled Pinned Locked Moved Web Development
tutorialjavascripthelpquestion
8 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bin_bin1
    wrote on last edited by
    #1

    Hello, there, I'd like to know whether there is a way to get/set caret position in textarea control in Javascript. I tried very hard to seach the Internet and didn't find anything useful. Please help me out. Here is an example of how I want it to work. Say I have two textarea boxes(TextArea1 and TextArea2) and a button. When the user clicks on the button, the text in the textArea1 is copied to textarea2, textarea1 lose focus and textarea2 will get the focus. All these are very easy to implement. But what I want is the caret in the TextArea2 set to the position of caret position in TextArea1 before the button click. Please help me. Thank you very much in advance. Bin

    S T 2 Replies Last reply
    0
    • B bin_bin1

      Hello, there, I'd like to know whether there is a way to get/set caret position in textarea control in Javascript. I tried very hard to seach the Internet and didn't find anything useful. Please help me out. Here is an example of how I want it to work. Say I have two textarea boxes(TextArea1 and TextArea2) and a button. When the user clicks on the button, the text in the textArea1 is copied to textarea2, textarea1 lose focus and textarea2 will get the focus. All these are very easy to implement. But what I want is the caret in the TextArea2 set to the position of caret position in TextArea1 before the button click. Please help me. Thank you very much in advance. Bin

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #2

      Check out the TextRange functions.

      Post faster, post more, post now

      B 1 Reply Last reply
      0
      • B bin_bin1

        Hello, there, I'd like to know whether there is a way to get/set caret position in textarea control in Javascript. I tried very hard to seach the Internet and didn't find anything useful. Please help me out. Here is an example of how I want it to work. Say I have two textarea boxes(TextArea1 and TextArea2) and a button. When the user clicks on the button, the text in the textArea1 is copied to textarea2, textarea1 lose focus and textarea2 will get the focus. All these are very easy to implement. But what I want is the caret in the TextArea2 set to the position of caret position in TextArea1 before the button click. Please help me. Thank you very much in advance. Bin

        T Offline
        T Offline
        T1TAN
        wrote on last edited by
        #3

        I suppose you could save the caret pos, something like this: var pos = TextArea1.caretPos; // do stuff TextArea2.caretPos = pos; I'm pretty sure this will work, but i'm not completely sure you can do this last step :doh: good luck;) --- http://sprdsoft.cmar-net.org - We Sprd You Softly Our site features contents and several images. Better check it out before the site grows even dumber.

        B 1 Reply Last reply
        0
        • S Shog9 0

          Check out the TextRange functions.

          Post faster, post more, post now

          B Offline
          B Offline
          bin_bin1
          wrote on last edited by
          #4

          Can you give me some details? I knew use TextRange too.

          S 1 Reply Last reply
          0
          • T T1TAN

            I suppose you could save the caret pos, something like this: var pos = TextArea1.caretPos; // do stuff TextArea2.caretPos = pos; I'm pretty sure this will work, but i'm not completely sure you can do this last step :doh: good luck;) --- http://sprdsoft.cmar-net.org - We Sprd You Softly Our site features contents and several images. Better check it out before the site grows even dumber.

            B Offline
            B Offline
            bin_bin1
            wrote on last edited by
            #5

            It is not working in IE. I know to do this in NN is easy, just like one line of code. But in IE, it is something different. var pos = TextArea1.caretPos; pos will be "undefined" after this line of code. Thank you very much.

            T 1 Reply Last reply
            0
            • B bin_bin1

              Can you give me some details? I knew use TextRange too.

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #6

              Here's a little example: <html> <head> <script> function DoTheCopy() {    var one = document.getElementById("FirstTextArea");    var two = document.getElementById("SecondTextArea");        one.focus();   // so cursor isn't on button just pressed    var range1 = document.selection.createRange();    var range2 = document.body.createTextRange();    range2.moveToElementText(one);        // count distance from start    var

              B 1 Reply Last reply
              0
              • S Shog9 0

                Here's a little example: <html> <head> <script> function DoTheCopy() {    var one = document.getElementById("FirstTextArea");    var two = document.getElementById("SecondTextArea");        one.focus();   // so cursor isn't on button just pressed    var range1 = document.selection.createRange();    var range2 = document.body.createTextRange();    range2.moveToElementText(one);        // count distance from start    var

                B Offline
                B Offline
                bin_bin1
                wrote on last edited by
                #7

                Thank you so much for your excellent code. It is exactly what I want. I have been trying to figure this out for two days. You really saved me. Thanks for your great help.

                1 Reply Last reply
                0
                • B bin_bin1

                  It is not working in IE. I know to do this in NN is easy, just like one line of code. But in IE, it is something different. var pos = TextArea1.caretPos; pos will be "undefined" after this line of code. Thank you very much.

                  T Offline
                  T Offline
                  T1TAN
                  wrote on last edited by
                  #8

                  Errr:doh:, this might seem silly, but have you tried anything like this: var pos; pos = TextArea1.caretPos; or even put var pos somewhere outside your function. I's worth a shot, considering IE's bug consistency.. ;) Another thing, you might try checking out CP's discussion board page source when you're posting stuff, they're also using JS for textarea (or so it seems:)) Good luck:cool: --- http://sprdsoft.cmar-net.org - We Sprd You Softly Our site features contents and several images. Better check it out before the site grows even dumber.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups