wrapText.js

/* ** Original Author: Taufik Nurrohman ** Original Snippet: http://jsfiddle.net/tovic/bd4np/ */ function wrapText(elementID, openTag, closeTag) { var textArea = $(elementID); var len = textArea.val().length; var start = textArea[0].selectionStart; var end = textArea[0].selectionEnd; var selectedText = textArea.val().substring(start, end); var replacement; if (closeTag === undefined) closeTag = openTag; if (selectedText === '') selectedText = 'SAMPLE_TEXT'; replacement = "["+openTag+"]"+ selectedText + "[/"+closeTag+"]"; textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len)); } // USAGE SAMPLES $('#myButton').click(function(){ wrapText('#choosenTextArea', 'b', 'b'); // result = [b]SAMPLE_TEXT[/b] // or wrapText('#choosenTextArea', 'b'); // result = [b]SAMPLE_TEXT[/b] // or wrapText('#choosenTextArea', 'url=URL_HERE', 'url'); // result = [url=URL_HERE]SAMPLE_TEXT[/url] });

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.