This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Inserting text at cursorposition else at the end of text in textarea in IE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Inserting text at cursorposition else at the end of text in textarea in IE" Watch "Inserting text at cursorposition else at the end of text in textarea in IE" New topic
Author

Inserting text at cursorposition else at the end of text in textarea in IE

venkateswara rao ravinuthala
Greenhorn

Joined: Feb 06, 2012
Posts: 7
<script>
function insertAtCursor(text) {
var field = document.getElementById("Expression");
if (document.selection) {
field.focus();
sel = document.selection.createRange();
sel.text = text;
}
else if (field.selectionStart || field.selectionStart == '0') {
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
field.value = field.value.substring(0, startPos)
+ text
+ field.value.substring(endPos, field.value.length);
} else {
field.value += text;
}
}
</script>
<body>
<textarea id="Expression" rows="5" cols="50">Put the caret in here and press the button or click outside and then press the button.</textarea>
<input type=button value=" ( " onclick="insertAtCursor('(');">
</body>

I am using the above function for appending the text in textarea when i click a button. In IE it is getting appended at the cursor position when cursor is placed else appending at the start of existing text in textarea.

I want to append the text at the end of existing text if cursor is not present else it should continue appending at the cursor position.

Please any one help me in this.
Thanks in advance.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Inserting text at cursorposition else at the end of text in textarea in IE
 
jQuery in Action, 3rd edition