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.
I think this shouldnt be difficult with jQuery. Look at these links:
- jQuery Tutorial
- inserting-a-text-where-cursor-is-using-javascript-jquery
- 10-jquery-and-non-jquery-javascript-rich-text-editors
精彩评论