PDA

View Full Version : Replacing selected Textarea text with something else


WebAsh
11-11-2005, 12:04 AM
Well... this is a last resort, kinda hoped i would have found it with some Google searches... but no... What i need is to be able to replace a portion of text within a textarea that is selected. I have got it working for Firefox (havent tried Opera yet) but when I went over to IE it didn't work at all. I need it for a rich text editor sort of interface like vBulletin and so on have.

Hope you can understand what i want... I'll post the javascript i have come up with so far that works in Firefox.

function bbcodeinsert (txtarea, code, attribute) {
txt = txtarea.value;
if ( document.all ) {
//IE CODE HERE!
} else if ( document.getElementById ) {
startselect = txtarea.selectionStart;
endselect = txtarea.selectionEnd;
/*alert(startselect);
alert(endselect);*/
if ( startselect != endselect) {
var selectedtext = txt.substring(startselect,endselect);
if ( !attribute ) {
extra = "";
} else {
extra = "=" & attribute;
}
bbcode = "[" + code + extra + "]" + selectedtext + "[/" + code + "]";
result = (txt.substring(0, startselect-1)) + bbcode + (txt.substring(endselect+1));
/*alert(txt);
alert(txtarea);
alert(code);
alert(attribute);
alert(selectedtext);
alert(extra);
alert(bbcode);
alert(result);*/
txtarea.value = result;
} else {
alert('Please select the text you want the action to be performed on');
}
}
}

DarkStreetDev
11-11-2005, 12:56 AM
Hey WebAsh - I know what your problem is. You have two conditions:
if(document.all) and if(document.getElementById)

Now, FireFox runs the code that's present in the (document.getElementById) condition, while IE and Opera will run that code that's present in the (document.all) condition. As you can see, you have no code in the (document.all) condition. All I did was to copy and paste the code in the (document.getElementById) condition and paste it where you say //IE CODE HERE! and it works 100% in FireFox, IE AND Opera!

WebAsh
11-11-2005, 01:12 AM
No you see it wasn't working without those... and i just had them there to show where I wanted the IE code to go... i did have some IE code that i tried... but it didnt work. It did in Opera which was strange. Both work in Opera.

Anyway, i think i have narrowed the problem down to IE doing something dicky with the element or something... because when i use the function i call it like so in a link:
"javascript:bbcodeinsert(document.getElementById('textareaid'), 'B', '');"
But IE returns nothing as being selected at all. When i tried that script you can see above with IE in a blank page with just a textare it worked fine... so who knows whats going on...

DarkStreetDev
11-11-2005, 01:47 AM
When I duplicated that code of yours into the document.all condition, it worked 100% in IE, FireFox and Opera, so I'm thinking the problem lies in the way you are calling the function. Do you have to send the textarea object as "document.getElementById('textareaid')"? Could you not rather say "document.formname.textareaid"? That should solve the problem.

WebAsh
11-11-2005, 03:52 AM
Yeah I'll try some more things of my own... thanks for your help DarkStreetDev :)