Inserting text into textbox at cursor position

Posted on 2009-05-13
Last Modified: 2013-11-08
In the simplest terms I have a button and a multiline textbox on my page. I type data into the textbox and when I click a button I need it to insert text at the cursor position.

This is also within an updatepanel. Is this possible?
Question by:syinner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 10

Expert Comment

ID: 24374787
can you use client-side script (JavaScript) or you are limited to server-side?

Author Comment

ID: 24374838
i got no problem using client side however this is within a control that is part of a content page... i have had problems with client side javascript when this is combined with an updatepanel.

i would like the server side to make the decision what needs to inserted through.
LVL 10

Accepted Solution

MaxOvrdrv2 earned 500 total points
ID: 24374912
the problem is that you cannot get the current cursor location from a textbox using server-side code... unless you are certain that the location will be either the beginning, or end of the text entered...  here's the JavaScript way of inserting text at the cursor location... this also gives you a selected text if you need it... use @pos@ var for current position:
function InsertAtLocation()
  var ctrl = 
  var saveText = ctrl.value;
  var range = document.selection.createRange();
  var specialchar = String.fromCharCode(1);
  range.text = specialchar;
  var pos = ctrl.value.indexOf(specialchar);
  ctrl.value = saveText;
  range = ctrl.createTextRange();
  range.move('character', pos);;
  window.event.returnValue = false;

Open in new window

LVL 10

Expert Comment

ID: 24374921
something like that anyway... for mine here i'm copying text from one text box to another... but you get the idea from the code anyway.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question