View Full Version : Javascript inserting bbcode

11-16-2010, 01:20 AM
Hello all,
I was making a bbcode powered posting system for my new site when I came along a problem. I have been working with Nile (well he's doing most of the coding, I'm just kind of a feedback system ;)) and his code works perfectly for Firefox, but alas, IE is very glitchy. Here is the code:

function bbSelected(myField, myValue){
var obj = document.getElementById(myField);
var s = obj.selectionStart;
var e = obj.selectionEnd;
var start = obj.value.slice(0, obj.selectionStart);
var end = obj.value.slice(obj.selectionEnd, obj.value.length);
var middle = obj.value.substring(obj.selectionStart, obj.selectionEnd);
var text = start + '['+myValue+']' + middle + '[/'+myValue+']' + end;
var text = start + '['+myValue+']' + middle + '[/'+myValue+']' + end;
document.getElementById(myField).value = text;
if(document.getElementById(myField).setSelectionRange){ //not IE
document.getElementById(myField).setSelectionRange(s+(myValue.length + 2), e+(myValue.length + 2));
} else if (document.getElementById(myField).createTextRange){
var range = document.getElementById(myField).createTextRange();
range.moveEnd('character', s+(myValue.length + 3));
range.moveStart('character', e+(myValue.length + 3));

Nile says that "the problem is probably with selectionStart and selectionEnd", but in any case Firefox works fine, IE well, does not. If you selected "quick" from "The quick brown dog" the output its:
the quick brown dogthe quick brown dogthe quick brown dog
This is really bizarre. What is going on and how can I fix this?