JavaScript gets textarea in the cursor position, add value

Source: Internet
Author: User

Not much to say directly on the code

Organize original: http://blog.csdn.net/liujin4049/article/details/1244065

<title>TEST</title>
<style>
Body
TD {
Font-family:verdana, ARIAL, Helvetica, Sans-serif;
font-size:12px;
}
</style>
<script type= "Text/javascript" >
var start = 0;
var end = 0;
function Add () {
var TextBox = document.getElementById ("Ta");
var pre = textBox.value.substr (0, start);
var post = TextBox.value.substr (end);
Textbox.value = pre + document.getElementById ("Inputtext"). Value + post;
}
function Savepos (TextBox) {
If it's Firefox (1.5), the method is simple.
if (typeof (Textbox.selectionstart) = = "Number") {
start = Textbox.selectionstart;
end = Textbox.selectionend;
}
Here is the IE (6.0) method, very troublesome, but also to calculate the '/n '
else if (document.selection) {
var range = Document.selection.createRange ();
if (Range.parentelement (). id = = textbox.id) {
Create a selection of the whole textarea
var range_all = Document.body.createTextRange ();
Range_all.movetoelementtext (TextBox);
Two range, one is the selected text (range), the other is the entire textarea (Range_all)
Range_all.compareendpoints () Compares two endpoints, if Range_all is more left (further to the right) than range, then//returns a value less than 0, then Range_all moves to the right point, Until the start of the two range is the same.
Calculate selection start point by moving beginning of Range_all to beginning of range
for (start = 0; Range_all.compareendpoints ("Starttostart", Range) < 0; start++)
Range_all.movestart (' character ', 1);
Get number of line breaks from textarea start to selection start and add them to start
Calculate/N
for (var i = 0; I <= start; i++) {
if (textBox.value.charAt (i) = = '/n ')
start++;
}
Create a selection of the whole textarea
var range_all = Document.body.createTextRange ();
Range_all.movetoelementtext (TextBox);
Calculate selection end point by moving beginning of Range_all to end of range
for (end = 0; range_all.compareendpoints (' Starttoend ', range) < 0; end++)
Range_all.movestart (' character ', 1);
Get number of line breaks from textarea start to selection end and add them to end
for (var i = 0; I <= end; i++) {
if (textBox.value.charAt (i) = = '/n ')
end++;
}
}
}
document.getElementById ("Start"). Value = start;
document.getElementById ("End"). Value = end;
}
</script>

<body>
<form action= "a.cgi" >
<table border= "1" cellspacing= "0" cellpadding= "0" >
<tr>
<td>start:
<input type= "text" id= "Start" size= "3"/>
</td>
<td>end:
<input type= "text" id= "End" size= "3"/>
</td>
</tr>
<tr>
<TD colspan= "2" >
<textarea id= "Ta" onkeydown= "Savepos (This)" onkeyup= "Savepos [This]" onmousedown= "Savepos (This)" onmouseup= " Savepos (This) "
Onfocus= "Savepos (This)" rows= "cols=" ></textarea>
</td>
</tr>
<tr>
<td>
<input type= "text" id= "Inputtext"/>
</td>
<td>
<input type= "button" onclick= "Add ()" value= "Add Text"/>
</td>
</tr>
</table>
</form>
</body>

JavaScript Gets the cursor location in textarea, add value

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.