Here we go again. I've been looking all day for a solution for this problem...
I got the code:
var formatString = function(t,style){
var el = $(t);
var pre;
var start;
var end;
var len;
var replace;
switch(style){
case 'italic':
pre = '[em]';
break;
case 'bold':
pre = '[b]';
break;
case 'paragraph':
pre = '[p]';
break;
}
if(document.selection){
el.focus();
var sel = document.selection.createRange();
// Finally replace the value of the selected text with this new replacement one
sel.text = pre + sel.text + pre.replace(/(\[)/,'[/');
} else {
len = el.val().length;
start = t.selectionStart;
end = t.selectionEnd;
sel = el.val().substring(start, end);
replace = pre + sel + pre.replace(/(\[)/,'[/');
el.val(el.val().substring(0,start) + replace + el.val().substring(end,len));
}
}
What I'm doing is creating a simple wysiwyg just to add some formating to the text that obviously is processed to show the correct html tags. All is working great of course with any browser but IE.
The problem that's happening that the beloved IE deselect any text selected when you click outside the textarea. And this is causing that the object sel.text has not text on it.
I run the function like this:
<button onclick="formatString($('#textid')[0],'italic')">Italic</button>
When you click the button other browsers output
'the italic text' // output the [em]italic[/em] text - when italic is selected
IE output 'the [em][/em]italic text'
If this is the same as another thread just guide me to it, literally I have read over 200 threads and nothing pointed out this problem.