Hi there

Im trying to figure out on how to add bbcode to an textarea when i click it

currently i use this

print "<textarea name='textarea' rows='5' cols='40'></textarea><br>";
print "<a onClick=\"addBBcode('[b][/b]')\"><b>Bold text</b></a> | ";
print "<a onClick=\"addBBcode('[i][/i]')\"><i>Italic text</i></a> | ";
 print "<a onClick=\"addBBcode('[u][/u]')\"><u>Underline text</u></a> | "; 

and this

<script language="javascript">
function addBBcode(textToAdd) {
     document.form.textarea.value += textToAdd;
     document.form.textarea.focus();
}
</script>

Its freakin basic and doesnt really support more than one textfield (ie. when there is 2 textarea's i want the bbcode to be added in the currently focussed field (the one with the cursor atm.)

Though my code does not support that 🙁

I cant find any good javascript reference so maybe some of you can help me out 🙂
also im total noob at javascript... so its hard figuring this one out

thanks in advance!

    ajdegans wrote:

    Hi there

    • Im trying to figure out on how to add bbcode to an textarea when i click it
    • I cant find any good javascript reference so maybe some of you can help me out

    ..... hard figuring this one out
    thanks in advance!

    hello ajdegans
    you can see how this php guy deals with bbcode and html
    he has some nice free php scripts for download in his archives

    This 'bbcode.parser.php' script is a part of Corz Blog

    you can download source code
    but also try out his scripts online

    Click 'try out this script' in BOTTOM Right corner of this page:

    corzblog.bbcode.parser.php at......... corz.org

    .... and dont forget visit 'PHP Research by halojoy' for latest and greatest links about php ....

    🆒

      yes, nice...

      but wheres the code to insert the bbcode into a form?

      i have written a bbcode already. i only need a way to use it in textareas as described above

        You do have a name for your form, right?

        print "<form name='form'>";
        

        Combined with the rest of your code, the entire thing works.

          yes, there is a name, and yes the current code works.

          but what i want is, when theres 2 textareas i want the bbcode to be added in the currently selected field and not the hardcoded one.

          so the javascript snippet needs to be changed. how?

            When the textarea comes into focus (onFocus attribute) run a javascript function that will update a hidden field in your form that has the textarea name in it. In your addBBCode function, had the name come from the hidden fields value e.t.c..

            <script language="javascript">
            function addBBcode(textToAdd) {
                var textareaName = document.form.textareaName.value;
                 document.form.textareaName.value += textToAdd;
                 document.form.textareaName.focus();
            }
            </script> 
            

              I understand now...I didn't get that from your original post; let's see if I can solve it this time :-)

              For the most part, I agree with Skull. This is what worked for me:

              <script language="javascript">
              function addBBcode(textToAdd) {
              	if (document.form.wfocus.value == "text1")
              	{
              	    document.form.textarea.value += textToAdd;
              		document.form.textarea.focus();
              	}
              	else
              	{
              	    document.form.textarea2.value += textToAdd;
              		document.form.textarea2.focus();
              	}
              }
              </script>
              
              <?php
              
              print "<form name='form'>";
              print "<input type='hidden' name='wfocus' value=''>";
              print "<textarea name='textarea' rows='5' cols='40' onfocus=\"document.form.wfocus.value='text1'\"></textarea><br>";
              print "<a onClick=\"addBBcode('[b][/b]')\"><b>Bold text</b></a> | ";
              print "<a onClick=\"addBBcode('[i][/i]')\"><i>Italic text</i></a> | ";
               print "<a onClick=\"addBBcode('[u][/u]')\"><u>Underline text</u></a> <br />"; 
              
               print "<textarea name='textarea2' rows='5' cols='40' onfocus=\"document.form.wfocus.value='text2'\"></textarea><br>";
              
               ?>
              
               

                oh oh oh,

                thats just plain amazing! coolness 🙂

                It works indeed

                Thanks a lot...

                gotta change all my files now though, hehe

                  gotta change all my files now though, hehe

                  That's why we have includes :-)

                  Good to hear it worked out for you. Hope everything else works out; if not, post it here!

                    Write a Reply...