Quote:
|
I've been fooling around with it and looking at tutorials, but I can't seem to get it
|
No it can be a real PITA to get working cross browser.
here's something I wrote earlier (I use it on my CMS system) complete with a bonus function to Prepend text as well as Insert (at cursor) and Append.
Code:
function insertText(p_oObj, p_sText) {
if (document.selection) {
p_oObj.focus();
var l_sSel = document.selection.createRange();
l_sSel.text = p_sText;
}
else if (p_oObj.selectionStart || p_oObj.selectionStart == '0') {
var l_iStartPos = p_oObj.selectionStart;
var l_iEndPos = p_oObj.selectionEnd;
p_oObj.value = p_oObj.value.substring(0, l_iStartPos)+ p_sText+ p_oObj.value.substring(l_iEndPos, p_oObj.value.length);
} else {
p_oObj.value += p_sText;
}
}
function appendText(p_oObj, p_sText) {
p_oObj.value += p_sText;
}
function prependText(p_oObj, p_sText) {
p_oObj.value = p_sText + p_oObj.value;
}
demo buttons;
HTML Code:
<button onclick="insertText(document.getElementById('textarea'),' Some text to insert ')">Insert some text</button>
<button onclick="appendText(document.getElementById('textarea'),' Some text to end ')">Append some text</button>
<button onclick="prependText(document.getElementById('textarea'),' Some text to front ')">Prepend some text</button>
Demo assumes the textarea has an ID of "textarea"
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|