I have a page where a user can edit the contents of an email that is sent out (the template).
Here is a look at what my page is like:
The Code:
<a name="tag" data-toggle="tooltip" tag="<<SubmitterFirst>>" title="The first name of the employee submitting the nomination"><<SubmitterFirst>></a>
...
$('[name=tag]').click(function(){
var caretPos = document.getElementById("templateContent").selectionStart,
currentTemplate = $('#templateContent').val(),
theTag = $(this).attr('tag');
$("#templateContent").empty().val(currentTemplate.substring(0, caretPos) + theTag + currentTemplate.substring(caretPos));
$("#templateContent").focus();
});
This seems to work fine in firefox with the adding of the tag at the cursor but when I do it in IE, it duplicates the whole template at the position.
I think the issue is happening here where its putting the content of the template back after adding the tag:
+ theTag + currentTemplate.substring(caretPos)
Any suggestions of how I can get this to work in IE or another suggestion?
See this jsFiddle for an example:
I have also tried the suggestions from the similar question here Inserting a text where cursor is using Javascript/jquery but none of them worked in IE8.
Here is an updated fiddle of another attempt. Works in FF but not IE8;
In ie8, it places the tag anywhere within the textarea, not where the cursor is.
UPDATE
This question solved my answer: Inserting text after cursor position in text areа which links to the jsfiddle of http://jsfiddle.net/rmDzu/2/