How would you like to be able to automatically quote
any selection of text from
anywhere on the Ticket page with the click of a button?
Quote: hello, i'm being quoted here.
This simple function will let you do just that.
Add this Javascript to the
top of the
ticket.html page.
<script language="JavaScript"><!--
function getSelectedText(){
if (window.getSelection){
txt = window.getSelection();
}
else if (document.getSelection) {
txt = document.getSelection();
}
else if (document.selection){
txt = document.selection.createRange().text;
}
else return;
return txt;
}
function makequote(bbopen, bbclose) {
var mytxt = getSelectedText();
var fld = document.getElementById('problem');
//trackme(fld);//
// IE
if (caretPos != null && fld.createTextRange) {
var range = caretPos;
//fld.focus();//
var wasempty = mytxt.length == 0 ? true : false;
range.text = bbopen + mytxt + bbclose;
if (wasempty) {
range.moveStart('character', -bbclose.length);
range.moveEnd('character', -bbclose.length);
range.select();
} else {
fld.focus(range);
}
// Not IE
} else if (typeof(fld.selectionStart) != "undefined") {
mytxt = String(mytxt);
var savescroll = fld.scrollTop;
var start = fld.selectionStart;
var end = fld.selectionEnd;
var txt = fld.value.substring(start, end);
if (txt.length != 0) {
fld.value = fld.value.substr(0, start) + bbopen + txt + bbclose + fld.value.substr(end);
} else {
fld.value = fld.value.substr(0, start) + bbopen + mytxt + bbclose + fld.value.substr(end);
}
var pos;
if (txt.length == 0 && mytxt.length == 0){
pos = start + bbopen.length;
} else if (mytxt.length != 0) {
pos = start + bbopen.length + mytxt.length + bbclose.length;
} else {
pos = start + bbopen.length + txt.length + bbclose.length;
}
fld.selectionStart = pos;
fld.selectionEnd = pos;
fld.focus();
fld.scrollTop = savescroll;
}
}
//-->
</script>
The add the "quote selection" button to the other BB Code buttons or anyplace else on your
ticket.html page.
<input name="quote" type="button" class="buttons" id="quote" tabindex="-1"
onclick="makequote('[b]Quote:[/b] [i]', '[/i]'); return false;" value="Quote Selection">
Verify that your text area also contains an ID. If not, just add the part in red. Just make sure it looks like this including the mouse events...
<textarea name="problem" id="problem" wrap="virtual" cols="70" rows="10" onselect="trackme(this);" onclick="trackme(this);" onkeyup="trackme(this);" onchange="trackme(this);">
It will work on text selected from
anyplace on the page including hi-lighted text from within the message box itself.
If you have something selected from one of the older messages, it will format it with all the proper tags and insert it at the cursor position of your message box.
If you select text from within your message box, it will format it with all the proper tags and place your cursor at the end of the new item.
If you have nothing selected, it puts your cursor in a position where you can just type in the text to be quoted.
Tested in IE, FireFox, & Safari which covers more than 98% or practically everyone.
Let me know if you find problems.