Javascript, textareas, selected text and IE

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
OasisGames
Forum Commoner
Posts: 26
Joined: Mon Apr 23, 2007 3:24 pm
Location: Ohio

Javascript, textareas, selected text and IE

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply