Howdy,
I have the following javascript code, which is supposed to help with some forums I am creating. When a ‘Bold’ button is clicked, the function will get the selected text and put a |B| around it.
[code=php]
function bold()
{
var userselection = “”;
if (window.getSelection) userselection = window.getSelection();
else if (document.getSelection) userselection = document.getSelection();
else if (document.selection) userselection = document.selection.createRange().text;
if (userselection == “”)
{
var Textin = prompt(“Enter the text you want to appear as bold:”, “”);
document.bheardform.message.value = document.bheardform.message.value + “|b|” + Textin + “|/b|”;
document.bheardform.message.focus();
}
else
{
NewContent = document.bheardform.message.value;
NewContent = NewContent.replace(userselection, “|B|” + userselection + “|/B|”);
document.bheardform.message.value = NewContent;
}
}
The code does work, but its limited. It gets the selected text, and replaces the occurence of that text in the message with the same text but with a |B|/|/B|around it. The problem is, if there are two occurences of the selected string in the message, the first is the one to get wrapped with the |B| tags, not neccesarily the selected one.
So, rather than using window.getSelection() (and its variants), are there functions I can use to get the position (both start and end) of the selected text, so I can replace it accordingly?
And what function (example please ?) would I use to replace a substring of text?
Thanks for any help, I really appreciate it!