I am building a template system for a client. They will have an area to type in a message that will be emailed out. I am providing them with a dropdownlist with a "Insert" button that will contain a list of values that they can insert into the message
that will be replaced when it is sent (firstname, lastname, etc..)
Here is my question, does anyone know how to make it to where when they click the "Insert" button is there a way for the browser to recognize where the cursor position is inside of the textbox and insert it exactly there.
A pipe "|" will be used to denote cursor position
----
Hello |, you are invited to .....
----
So the user typed out his message and then put the cursor where the | is currently... I want when they click the insert button the value [FirstName] will get entered in that exact location. I am currently using a standard asp text-box for user input.
Has anyone played with this before, thanks for help as always.
I'm sure there are javascript solutions to this (which I've never explored). I would tend to use a server-side replace on the dropdownselectedindexchanged event.
so something like
replace("|","whatever they chose","whatever you want it to say")
That'll work if there is only one field to be replaced but if there are multiple replacements, I always go with something like BBCODE. e.g.
My name is [name], I am [age], I love to [something legal]
Then you just use
replace("[name]","whatever they chose","whatever you want it to say")
Everyone's a noob at something...Mark a post as 'Answer' if it helps, it makes us feel good about ourselves.
Web Design Tameside
parackson
Member
209 Points
99 Posts
TextBox Insert Text at Cursor Location
May 20, 2009 08:00 PM|LINK
I am building a template system for a client. They will have an area to type in a message that will be emailed out. I am providing them with a dropdownlist with a "Insert" button that will contain a list of values that they can insert into the message that will be replaced when it is sent (firstname, lastname, etc..)
Here is my question, does anyone know how to make it to where when they click the "Insert" button is there a way for the browser to recognize where the cursor position is inside of the textbox and insert it exactly there.
A pipe "|" will be used to denote cursor position
----
Hello |, you are invited to .....
----
So the user typed out his message and then put the cursor where the | is currently... I want when they click the insert button the value [FirstName] will get entered in that exact location. I am currently using a standard asp text-box for user input.
Has anyone played with this before, thanks for help as always.
VS2005, 2.0
walsharoo
Participant
979 Points
211 Posts
Re: TextBox Insert Text at Cursor Location
May 20, 2009 10:47 PM|LINK
I'm sure there are javascript solutions to this (which I've never explored). I would tend to use a server-side replace on the dropdownselectedindexchanged event.
so something like
replace("|","whatever they chose","whatever you want it to say")
That'll work if there is only one field to be replaced but if there are multiple replacements, I always go with something like BBCODE. e.g.
My name is [name], I am [age], I love to [something legal]
Then you just use
replace("[name]","whatever they chose","whatever you want it to say")
Web Design Tameside
parackson
Member
209 Points
99 Posts
Re: TextBox Insert Text at Cursor Location
May 20, 2009 10:49 PM|LINK
this looks like the answer
http://aspalliance.com/1109_CodeSnip_Inserting_Text_at_Cursor_Location_Using_JavaScript
Tim Cooke
Member
4 Points
2 Posts
Re: TextBox Insert Text at Cursor Location
Feb 14, 2013 01:01 AM|LINK
I have been using an abbreviated form of this (which has also been mentioned on other threads):
if (document.selection) { $(input).focus(); sel = document.selection.createRange(); sel.text = valueString; }It worked fine in IE 8, but seems to be broken in IE 10. Any ideas? Thanks in advance!
Tim Cooke
Member
4 Points
2 Posts
Re: TextBox Insert Text at Cursor Location
Feb 15, 2013 09:59 PM|LINK
Sorry for the misfire. The problem was not with the above code but in an apparent change of the event object.