Dragon707 Posted November 24, 2008 Share Posted November 24, 2008 Hello all, I just have a java script that i use for my texteditor but for insert the UBB codes go something wrong. When i select some text and i use UBBCode('b') than is the output wrong al is gone but the selected isnt gone. Script: <script language="JavaScript"> function UBBCode(UBB){ var SELECT=(document.rte.tekst.value).substring(document.rte.tekst.selectionStart,document.rte.tekst.selectionEnd); var edit='['+UBB+']'+SELECT+'[/'+UBB+']'; document.rte.tekst.value=edit; document.rte.tekst.focus(); return; } </script> I hope that somebody can help me, PS: Sorry for my bad english. I am dutch Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 25, 2008 Share Posted November 25, 2008 I would also pass the field ID to the function instead of hard codsing the field intot he function to make it more flexible. The script also needs to get the text before and after the selected text to do what you want. This corrects that - however selectionStart and selectionEnd do not work in IE. <html> <head> <script type="text/javascript"> function UBBCode(fieldID, UBB) { fieldObj = document.getElementById(fieldID); var BEFORE = (fieldObj.value).substring(0, fieldObj.selectionStart); var SELECT = (fieldObj.value).substring(fieldObj.selectionStart, fieldObj.selectionEnd); var AFTER = (fieldObj.value).substring(fieldObj.selectionEnd); fieldObj.value = BEFORE + '['+UBB+']' + SELECT + '[/'+UBB+']' + AFTER; fieldObj.focus(); return; } </script> </head> <body> <textarea id="tekst"></textarea> <button onclick="UBBCode('tekst', 'B')";>TEST</button> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.