1

I have a textbox in aspx page in which the user enters text. Now when user clicks on a button called "Sin" the textbox should show "Sin[]" and the cursor has to be placed in between brackets.Like as follows "Sin[<cursor here>]" Now when the user clicks on some other button say "Cos" the textbox text should show "Sin[Cos[]]" and the cursor has be placed between the brackets of Cos as follows: "Sin[Cos[<cursor here>]]".

How is this handled. Any simple code please..

Thanks in advance

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
Anil
  • 21
  • 4

2 Answers2

0

I hope you are using jquery.

<asp:TextBox id="txt" runat="server" />
<input type="button" id="sinBtn" value="Sin" tag="Sin[<cursor here>]" />
<input type="button" id="cosBtn" value="Cos" tag="Cos[<cursor here>]" />
//also you can generate the two buttons with server controls

Here is the javascript:

$(document).ready(function(){
   $('input[tag]').click(function(){
        var theText = $('#<%= txt.ClientID %>');
        theText.txt(theText.txt().replace('<cursor here>',this.tag);
   })
});

If you don't have a string.replace method, extend one using prototype.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • Hi Thanks for your reply. the code you provided doesn't work. Can u please be descriptive. – Anil Jun 04 '10 at 07:54
0

I think this shouldnt be difficult with jQuery. Look at these links:

  1. jQuery Tutorial
  2. inserting-a-text-where-cursor-is-using-javascript-jquery
  3. 10-jquery-and-non-jquery-javascript-rich-text-editors
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939