简体   繁体   中英

Inserting text in TextBox at cursor

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM