chocopi Posted June 11, 2007 Share Posted June 11, 2007 I have a little bbcode in php where is the user posts something like [b][/b] then it will be replaced with <b></b> and that works fine. I have searched google and the first few pages of this section, but i cant find anything. What i want is, when the user clicks the button it is inserted into the textarea so th 'b' button would insert [b][/b], basically what this site has. So if anyone can help, it would be greatly appreciated ~ Chocopi Quote Link to comment Share on other sites More sharing options...
lighton Posted June 12, 2007 Share Posted June 12, 2007 onclick="insertbold()" insertbold() { document.form[0].textareaname.value += "<b><b/>"; } something like that might work assuming you have only one form in the page, im a bit of a javascript neebie myself so dont know if this is right give it a whirl. Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 this is such a n00bish question, but i havent used javascript in years. where do i place the stuff <html> <body> <form name="name" method="post" action="<?php $PHP_SELF ?>"> <center> <br /> <input type="text" name="subject" id="subject" value="<?php $subject ?>" /> <br /> <br /> <br /> <textarea name="message" value="<?php $original ?>" cols="60" rows="15"></textarea> <br /> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> </center> </body> </html> Thanks ~ Chocopi Quote Link to comment Share on other sites More sharing options...
lighton Posted June 14, 2007 Share Posted June 14, 2007 this would go in your html form <input type="button" onclick="bold()" /> include a javascript page in your head write the javascript function in the seperate javascript page function bold() { form[0].message.value += <b></b>; } tihs might work althuogh you might have to find the form with document.getElememtById('form_name') give it a whirl Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 14, 2007 Author Share Posted June 14, 2007 ok i have this now, but it does nothing <html> <head> <script> function bold() { document.getElememtById('message').message.value += ; } </script> </head> <body> <form name="name" method="post" action="<?php $PHP_SELF ?>"> <center> <br /> <input type="button" value="Bold" onclick="bold()" /> <br /> <br /> <input type="text" name="subject" id="subject" value="<?php $subject ?>" /> <br /> <br /> <br /> <textarea name="message" value="<?php $original ?>" cols="60" rows="15"></textarea> <br /> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> <br /> <br /> <a href="view.php">View Forum</a> </center> </body> </html> Thanks lighton, ~ Chocopi Quote Link to comment Share on other sites More sharing options...
lighton Posted June 15, 2007 Share Posted June 15, 2007 i am a bit of a javascript newbie myself but you need to add "<b></b>" to you += bit and you will probably need to use document.forms[0].messagearea.value or it could be form[0] (without the s) try that out Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 15, 2007 Author Share Posted June 15, 2007 Thanks, but its still not showing anything Quote Link to comment Share on other sites More sharing options...
lighton Posted June 15, 2007 Share Posted June 15, 2007 you need to add the actual < b > bold markup to the javascript Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 17, 2007 Author Share Posted June 17, 2007 where abouts Quote Link to comment Share on other sites More sharing options...
Henaro Posted June 18, 2007 Share Posted June 18, 2007 I had this same problem awhile back and I eventually found this function somewhere: <script language="JavaScript" type="text/JavaScript"> function sendText(e, text) { e.value+=text } </script> I can't exactly remember where I found it but oh well. A way it can be used is: <a href='#' onClick="sendText(document.FORMNAMEHERE.TEXTAREANAMEHERE, '[b][/b]')" >[b][/b]</a> Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 18, 2007 Author Share Posted June 18, 2007 Yea that works, Thanks ! Do you know how to make it work where you highlight a word/words and when you click it puts the code around it ? Quote Link to comment Share on other sites More sharing options...
Henaro Posted June 18, 2007 Share Posted June 18, 2007 Sadly I don't. If you find out please tell me though. EDIT: Actually I just go curious and decided to look at this site's javascript. I found this: function surroundText(text1, text2, textarea) { // Can a text range be created? if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos, temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); } // Mozilla text range wrap. else if (typeof(textarea.selectionStart) != "undefined") { var begin = textarea.value.substr(0, textarea.selectionStart); var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart); var end = textarea.value.substr(textarea.selectionEnd); var newCursorPos = textarea.selectionStart; var scrollPos = textarea.scrollTop; textarea.value = begin + text1 + selection + text2 + end; if (textarea.setSelectionRange) { if (selection.length == 0) textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length); else textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length); textarea.focus(); } textarea.scrollTop = scrollPos; } // Just put them on the end, then. else { textarea.value += text1 + text2; textarea.focus(textarea.value.length - 1); } } That's what the use for their bold and image tags. Used the same way as the other function. Example: <a href="#" onclick="surroundText('[img=', ']', document.forms.FORMNAMEHERE.TEXTAREANAMEHERE); return false;"> Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 18, 2007 Author Share Posted June 18, 2007 Cheers That works perfectly ~ Chocopi 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.