Page 1 of 1

Javascript, textareas, selected text and IE

Posted: Fri Jun 08, 2007 10:11 am
by OasisGames
I have a textarea that I want to grab the selected text from in all browsers for use in a bbCode insertion tool.
The code I have:

Code: Select all

function addCode(code,codeclose) {
var Text = document.form.content.value;
var selectedText = Text.substring(document.form.content.selectionStart, document.form.content.selectionEnd);
var beforeSelected = Text.substring(0,document.form.content.selectionStart);
var afterSelected = Text.substring(document.form.content.selectionEnd,Text.length);
document.form.content.value = beforeSelected+code+selectedText+codeclose+afterSelected;
}

Code: Select all

javascript:addCode('[b]','[/b]')
Works fine in Firefox, but doubles over (uses all text as selected region) in Internet Explorer.
Any suggestions on fixing the problem for IE?

Posted: Fri Jun 08, 2007 6:23 pm
by superdezign
Well, textareas possess an innerHTML property and, seeing as Microsoft invented it, maybe you should try using it instead of the "value" property. Not sure if it'll do it, but it certainly might.