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
  • 3
LVL 10

Expert Comment

Comment Utility
can you use client-side script (JavaScript) or you are limited to server-side?

Author Comment

Comment Utility
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
Comment Utility
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

Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now