By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

js part of opperating the bbcode

100+
P: 290
Hi,
I want to write a js script that will wrap any text that is hıghighted in a textarea with the bbcode bold code or italics etc.

The same way that this forum post script must work.
(actually if I could find the script I could then modify it to suit my needs which would be easier )

Anyway - I am not sure how to start because how do I detect what characters are highlighted ?.

I have looked in the source code of this page for the js but can't find it :(

Can anyone help out.
Thanks
Jan 18 '09 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027

P: 33
I use this, its from simpleBBCode by Brady Mulhollem, the function accepts the open and close tags.

Expand|Select|Wrap|Line Numbers
  1. function addTags(Tag, fTag) 
  2.   var obj = document.formname.element; 
  3.   obj.focus(); 
  4.  
  5.   if (document.selection && document.selection.createRange)  // Internet Explorer 
  6.   { 
  7.     sel = document.selection.createRange(); 
  8.     if (sel.parentElement() == obj)  sel.text = Tag + sel.text + fTag; 
  9.   } 
  10.  
  11.   else if (obj != "undefined")  // Firefox 
  12.   { 
  13.         var longueur = parseInt(obj.textLength); 
  14.     var selStart = obj.selectionStart; 
  15.     var selEnd = obj.selectionEnd; 
  16.  
  17.     obj.value = obj.value.substring(0,selStart) + Tag + obj.value.substring(selStart,selEnd) + fTag; 
  18.   } 
  19.  
  20.   else obj.value += Tag + fTag; 
  21.  
  22.   obj.focus(); 
  23.  
Jan 19 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.