Original Post
I'm having difficulty finding suitable pages about this on google. Basically I'm writing a simple editor and I want to add a few buttons that will add tags around the current selection for formatting. So far I have this piece of javascript to apply bold to the selected piece of text. I have a few questions.. (1) How can I only affect the selection if it is within the textarea named 'text' on the form named 'editor'? (2) If there is no selection, or the 'text' textarea doesn't currently have keyboard focus, how can I detect this and use it to simply add "" into the textarea at the location of the cursor? (i.e. if I click at the start of the textarea, then click in another text box, then call ApplyBold() via a button, ApplyBold will insert the strong tag at the start of the 'text' textarea). Thanks!
function ApplyBold()
{
theSelection = document.selection.createRange().text;
if(theSelection)
{
document.selection.createRange().text = "" + theSelection + "";
document.selection.anchorOffset = selStart;
}
}