Important alert: (current site time 7/15/2013 1:28:02 PM EDT)
 

VB icon

Apply BBCode to Textarea

Email
Submitted on: 7/18/2008 12:33:30 PM
By: Matt DeKok  
Level: Intermediate
User Rating: Unrated
Compatibility: JavaScript
Views: 5027
author picture
(About the author)
 
     This script will insert some bbcode where the selection is located in a textarea form field. There is also an additional function for link bbcode with prompt feature.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Apply BBCode to Textarea
'**************************************
<html>
<head>
<style type="text/css">
.bbButton {
	height: 30px;
	font-family: serif;
	font-size: 16pt;
}
</style>
</head>
<body>
<input type="button" value="B" onclick="addtag('b')" class="bbButton" style="width:30px; font-weight:bold;" />
<input type="button" value="I" onclick="addtag('i')" class="bbButton" style="width:30px; font-style:italic;" />
<input type="button" value="U" onclick="addtag('u')" class="bbButton" style="width:30px; text-decoration:underline;" />
<input type="button" value="Link" onclick="addurltag()" class="bbButton" />
<input type="button" value="List" onclick="addtag('list')" class="bbButton" />
<input type="button" value="Code" onclick="addtag('code')" class="bbButton" /><br />
<br />
<textarea id="mta" name="mta" cols="40" rows="7"></textarea>
</body>
</html>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Apply BBCode to Textarea
' Description:This script will insert some bbcode where the selection is located in a textarea form field. There is also an additional function for link bbcode with prompt feature.
' By: Matt DeKok
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6058&lngWId=14'for details.'**************************************

function addtag(tag) {
	var txt = document.getElementById('mta');
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + sel.text + '[/' + tag + ']';
	} else if(txt.selectionStart || txt.selectionStart == '0') {	
		txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"]" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	} else {
		txt.value = '[' + tag + '][/' + tag + ']';
	}
	return;
}
function addurltag() {
	var txt = document.getElementById('mta');
	var link = prompt("Type the address:", "http://");
	if(link.length == 0 || link == "http://") {
		return;
	} else {
		var link = "=" + link;
		var text;
		var sel2 = "";
		if(document.selection) {
			txt.focus();
			sel = document.selection.createRange();
			sel2 = sel.text;
		} else if(txt.selectionStart || txt.selectionStart == '0') {
			sel2 = (txt.value).substring(txt.selectionStart, txt.selectionEnd);
		}
		if(sel2.length > 0) {
			text = sel2;
		} else {
			text = prompt("Enter the link text:", "");
		}
	}
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = "[url" + link + "]" + text + "[/url]";
	} else {
		txt.value = (txt.value).substring(0, txt.selectionStart) + "[url" + link + "]" + text + "[/url]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	}
	return;
}


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.