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.
- Varstart = 0;
- Varend = 0;
- Functionadd (){
- VartextBox = document. getElementById ("ta ");
- Varpre = textBox. value. substr (0, start );
- Varpost = textBox. value. substr (end );
- TextBox. value = pre + document.
- GetElementById ("inputtext"). value + post;
- }
- FunctionsavePos (textBox ){
- // If it is Firefox (1.5), the method is simple.
- If (typeof (textBox. selectionStart) = "number "){
- Start = textBox. selectionStart;
- End = textBox. selectionEnd;
- }
- // The following is the IE (6.0) method, which is very troublesome and must be calculated as '\ n'
- Elseif (document. selection ){
- Varrange = document. selection. createRange ();
- If (range. parentElement (). id = textBox. id ){
- // Createaselectionofthewholetextarea
- Varrange_all = document. body. createTextRange ();
- Range_all.moveToElementText (textBox );
- // Two ranges. One is the selected text (range ),
- One is the entire textarea (range_all)
- // Range_all.compareEndPoints () compares two endpoints,
- If range_all is greater than range to left (furthertotheleft ),
- 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.
- // Calculateselectionstartpointbymoving
- Beginningofrange_alltobeginningofrange
- For (start = 0; range_all.compareEndPoints
- ("StartToStart", range) <0; start ++) range_all.moveStart ('character ', 1 );
- // Getnumberoflinebreaksfromtextareastarttose
- Lectionstartandaddthemtostart
- // Calculate \ n
- For (vari = 0; I <= start; I ++ ){
- If (textBox. value. charAt (I) = '\ n ')
- Start ++;
- }
- // Createaselectionofthewholetextarea
- Varrange_all = document. body. createTextRange ();
- Range_all.moveToElementText (textBox );
- // Calculateselectionendpointbymovingbeginningofrange_alltoendofrange
- For (end = 0; range_all.compareEndPoints ('starttoend', range) <0; end ++)
- Range_all.moveStart ('character ', 1 );
- // Getnumberoflinebreaksfromtextareastarttoselectionendandaddthemtoend
- For (vari = 0; I <= end; I ++ ){
- If (textBox. value. charAt (I) = '\ n ')
- End ++;
- }
- }
- }
- Document. getElementById ("start"). value = start;
- Document. getElementById ("end"). value = end;
- }
-
The following describes how to call js Code on the page:
- <formactionformaction="a.cgi">
- <tablebordertableborder="1"
- cellspacing="0"cellpadding="0">
- <tr>
- <td>start:<inputtypeinputtype="text"
- id="start"size="3"/></td>
- <td>end:<inputtypeinputtype="text"
- id="end"size="3"/></td>
- </tr>
- <tr>
- <tdcolspantdcolspan="2">
- <textareaidtextareaid="ta"onKeydown="savePos(this)"
- onKeyup="savePos(this)"
- onmousedown="savePos(this)"
- onmouseup="savePos(this)"
- onfocus="savePos(this)"
- rows="14"cols="50"></textarea>
- </td>
- </tr>
- <tr>
- <td><inputtypeinputtype="text"
- id="inputtext"/></td>
- <td><inputtypeinputtype="button"
- onClick="add()"value="AddText"/></td>
- </tr>
- </table>
- </form>