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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/ 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. Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-273001 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-273068 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-274479 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-274747 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-275203 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-275274 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 Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-275393 Share on other sites More sharing options...
chocopi Posted June 17, 2007 Author Share Posted June 17, 2007 where abouts Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-276239 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> Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-276639 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 ? Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-276642 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;"> Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-276647 Share on other sites More sharing options...
chocopi Posted June 18, 2007 Author Share Posted June 18, 2007 Cheers That works perfectly ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/55066-solved-button-into-textarea/#findComment-276661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.