Technology sharing how Javascript gets the cursor position in textarea

Source: Internet
Author: User

Here we will share with you how to use Javascript to obtain the cursor position in textarea. I believe this article will surely help you gain some benefits.

Use Javascript to obtain the cursor position in textarea

Javascript has always been known for its flexibility and randomness, which makes its functions very powerful. However, it is difficult to use it because it does not have a good debugging tool, this is especially difficult for beginners. The question discussed today is to use javascript to obtain the cursor position in textarea. For those who write javascript into the Web Editor, obtaining the cursor position in textarea is a very important issue. Many people are often at a loss and cannot find a good solution. Yesterday, I found a piece of javascript code on the Internet. I didn't want to put the original version here because it was so wonderful that I was afraid that it would be broken. So I should leave the original version here.

 
 
  1. Varstart = 0;
  2. Varend = 0;
  3. Functionadd (){
  4. VartextBox = document. getElementById ("ta ");
  5. Varpre = textBox. value. substr (0, start );
  6. Varpost = textBox. value. substr (end );
  7. TextBox. value = pre + document.
  8. GetElementById ("inputtext"). value + post;
  9. }
  10. FunctionsavePos (textBox ){
  11. // If it is Firefox (1.5), the method is simple.
  12. If (typeof (textBox. selectionStart) = "number "){
  13. Start = textBox. selectionStart;
  14. End = textBox. selectionEnd;
  15. }
  16. // The following is the IE (6.0) method, which is very troublesome and must be calculated as '\ n'
  17. Elseif (document. selection ){
  18. Varrange = document. selection. createRange ();
  19. If (range. parentElement (). id = textBox. id ){
  20. // Createaselectionofthewholetextarea
  21. Varrange_all = document. body. createTextRange ();
  22. Range_all.moveToElementText (textBox );
  23. // Two ranges. One is the selected text (range ),
  24. One is the entire textarea (range_all)
  25. // Range_all.compareEndPoints () compares two endpoints,
  26. If range_all is greater than range to left (furthertotheleft ),
  27. Then // If the returned value is less than 0, range_all is shifted to the right until the start values of the two ranges are the same.
  28. // Calculateselectionstartpointbymoving
  29. Beginningofrange_alltobeginningofrange
  30. For (start = 0; range_all.compareEndPoints
  31. ("StartToStart", range) <0; start ++) range_all.moveStart ('character ', 1 );
  32. // Getnumberoflinebreaksfromtextareastarttose
  33. Lectionstartandaddthemtostart
  34. // Calculate \ n
  35. For (vari = 0; I <= start; I ++ ){
  36. If (textBox. value. charAt (I) = '\ n ')
  37. Start ++;
  38. }
  39. // Createaselectionofthewholetextarea
  40. Varrange_all = document. body. createTextRange ();
  41. Range_all.moveToElementText (textBox );
  42. // Calculateselectionendpointbymovingbeginningofrange_alltoendofrange
  43. For (end = 0; range_all.compareEndPoints ('starttoend', range) <0; end ++)
  44. Range_all.moveStart ('character ', 1 );
  45. // Getnumberoflinebreaksfromtextareastarttoselectionendandaddthemtoend
  46. For (vari = 0; I <= end; I ++ ){
  47. If (textBox. value. charAt (I) = '\ n ')
  48. End ++;
  49. }
  50. }
  51. }
  52. Document. getElementById ("start"). value = start;
  53. Document. getElementById ("end"). value = end;
  54. }
  55.  

The following describes how to call js Code on the page:

 
 
  1. <formactionformaction="a.cgi"> 
  2. <tablebordertableborder="1"
  3. cellspacing="0"cellpadding="0"> 
  4. <tr> 
  5. <td>start:<inputtypeinputtype="text"
  6. id="start"size="3"/></td> 
  7. <td>end:<inputtypeinputtype="text"
  8. id="end"size="3"/></td> 
  9. </tr> 
  10. <tr> 
  11. <tdcolspantdcolspan="2"> 
  12. <textareaidtextareaid="ta"onKeydown="savePos(this)"  
  13. onKeyup="savePos(this)" 
  14. onmousedown="savePos(this)" 
  15. onmouseup="savePos(this)" 
  16. onfocus="savePos(this)" 
  17. rows="14"cols="50"></textarea> 
  18. </td> 
  19. </tr> 
  20. <tr> 
  21. <td><inputtypeinputtype="text"
  22. id="inputtext"/></td> 
  23. <td><inputtypeinputtype="button"
  24. onClick="add()"value="AddText"/></td> 
  25. </tr> 
  26. </table> 
  27. </form> 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.