#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    328
    Rep Power
    14

    Formatting selected text in a texterea w/ JavaScript


    As a PHP programmer i'm not very pleased with JavaScript, both because I suck at it and because the language is crappy (no offence). However, sometimes JavaScript is vital and I just have to use it.

    I'm working on some kind of online editor, similar to the one used by this forum. So you will have to be able to select a text in a textarea and then click a button (like B]B[/B]) to format a selection of text into the prefered style.

    I've wrote a bit, which isn't working at all. Could someone please tell me what's going wrong? Because my particular JavaScript skills aren't very good. Also please warn me for browser bugs in IE, because I have only Mozilla installed at this system and I would like to eliminate bugs before I have the code tested on different platforms.

    Here is the code (stripped version):

    PHP Code:
    function form()
        {
            echo 
    "\n<script type=\"text/javascript\">
                    
            function notSupported()
            { 
                alert('your browser is not supported.');
            }
            
            function setTag(tag)
            {
                if(text.setSelectionRange)
                {
                    var start=parseInt(text.start.value);
                    var end  =parseInt(text.end  .value);
                    text.setSelectionRange(start,end);
                    text.focus();
                    selection = text.value.substr(text.selectionStart,text.selectionEnd-text.selectionStart);
                    document.editor.text.value = \"[\" + tag + \"]\" + selection + \"[/\" + tag + \"]\";
            
                } 
            else notSupported();
            }
            //--></script>
            
            <form id=\"editor\" name=\"editor\" action=\""
    .$_SERVER['PHP_SELF']."=\" method=\"post\">";
            
            echo 
    "<div class=\"editor_bottom\">
            <textarea name=\"text\" cols=\"90\" rows=\"20\"></textarea><br />
            <input type=\"button\" value=\"B\" class=\"button_bold\" onClick=\"setTag('b')\" />
            </div>
            </form>"
    ;
        } 
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Barcelona
    Posts
    133
    Rep Power
    12
    do a search in google for IE versions of range scripts. For mozilla's version go to the official one at http://www.mozilla.org/editor/midas-spec.html
    good luck
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Barcelona
    Posts
    133
    Rep Power
    12
    btw I'd better try to learn some javascript before saying is crappy (no offense either)

IMN logo majestic logo threadwatch logo seochat tools logo