I’m swapping selected text in a textarea to replace it with something else.
Got it working using replace() but the problem is it replaces the first position of the selected in the string rather than the actual position.
ie. if the string has two of the same words in it and I wanted to replace the second one, it replaces the first.
[CODE]
function makelink(txtarea) {
var sl = (txtarea.value).substring(txtarea.selectionStart,txtarea.selectionEnd);
if (sl==”) {
alert(‘First Select Some Text!’);
return false;
}
var url = prompt (“Enter URL:”,””);
var link = “<a href=””+url+””>”+sl+”</a>”;
var tx = txtarea.value;
tx=tx.replace(sl, link);
txtarea.value = tx;
}