Forum Moderators: open

Message Too Old, No Replies

Inserting at cursor / turning selected text into a variable

         

Readie

3:54 am on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been using a script to insert BB code into a text area on a button click (much like the nice shiny new ones I see here :)) and it was working fine... Until I moved the script into an external .js due to me creating 2 more pages that would be using it.

Now, it still inserts at the cursor fine, but is no longer recognising that there is any text selected, and is instead inserting "undefined" between the tags, over the top of any selected text(if any is selected)

The code is as follows:

function insertAtCursor(bb_tex, bb_val) {
if (window.document.selection) {
bb_tex.focus();
sel = window.document.selection.createRange();
sel.text = bb_val;
} else if (bb_tex.selectionStart || bb_tex.selectionStart == '0') {
var startPos = bb_tex.selectionStart;
var endPos = bb_tex.selectionEnd;
bb_tex.value = bb_tex.value.substring(0, startPos)
+ bb_val
+ bb_tex.value.substring(endPos, bb_tex.value.length);
} else {
bb_tex.value += bb_val;
}
}

var st = window.document.selection.createRange().text;

and then the function would be used like:

insertAtCursor(window.document.newspost.content, '[u]' + st + '[/u]');

It's probably something really simple, but I'm too much of a noob at JS to work it out :S

Anyway, any help anyone could offer would be much appreciated :)

whoisgregg

10:34 pm on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've looked it over but nothing is jumping out at me. :/ Any luck sorting this out?