Javascript Text Wrapping Function
The function takes the selected text (in a textarea field), wraps it with the appropriate BBCode tag (set by the type variable) and inserts it back where it was. If no text is selected, then it simply appends a blank BBCode block to the bottom of the textarea. It could probably stand to be rewritten in jQuery, but I don’t have time right now.
function wrapText(type) { var textarea = document.forms.edit.elements.pcopy; var caretPos = textarea.selectionStart; var caretEnd = textarea.selectionEnd; var value = textarea.value; if (caretPos < caretEnd) { textarea.value = value.substring(0, caretPos) + //all the text before the selection '[' + type + ']' + //bbcode start value.substring(caretPos, caretEnd) + //selected text '[/' + type + ']' + //bbcode end value.substring(caretEnd, value.length); //remaining textarea content textarea.focus(); } else { var bbcode = '[' + type + '][/' + type + ']'; textarea.value = value.substr(0, caretPos) + bbcode + value.substr(caretPos); textarea.focus(); textarea.selectionStart = caretPos + 3; textarea.selectionEnd = caretPos + 3; } }
Leave a Reply
Contact Us
Search this Site
Archives
- October 2009 (1)
- September 2009 (2)
- August 2009 (2)
- June 2009 (1)
- May 2009 (1)
- March 2009 (2)
- February 2009 (3)
- January 2009 (5)
- December 2008 (1)
- November 2008 (2)
- October 2008 (3)
- September 2008 (4)
- August 2008 (4)






