Results 1 to 5 of 5

Thread: Very very basic & crossbrowser rich-text editor

  1. #1
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very very basic & crossbrowser rich-text editor

    i want to make an very very basic rich-text editor, which works in all browser types.

    i have the code at below.. but it only works on -8 IE browsers.
    could someone help me to make this code crossbrowser..
    Code:
    <script type="text/javascript">
    <!--
        function formatText (tag) {
            var selectedText = document.selection.createRange().text;
            
            if (selectedText != "") {
                var newText = "[" + tag + "]" + selectedText + "[/" + tag + "]";
                document.selection.createRange().text = newText;
            }
        }
    //-->
    </script>
    
    <form name="my_form">
        <textarea name="my_textarea"></textarea><br />
        <input type="button" value="bold" onclick="formatText ('b');" />
        <input type="button" value="italic" onclick="formatText ('i');" />
        <input type="button" value="link" onclick="formatText ('link');" />
    </form>

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Could try looking through this to see how nicedit does it.

    http://js.nicedit.com/nicEdit-latest.js

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    See this entire thread:

    http://www.dynamicdrive.com/forums/s...ad.php?t=50020

    Nothing pretty, but it shows the basic cross browser methods for this sort of thing.
    Last edited by jscheuer1; 12-17-2009 at 08:22 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i did it like this... and its working crossbrowser..
    but.. it's adding tags to the before/after to all writtens in textarea... (document.getElementById('textarea'))

    It must add the tags before/after to the only sellected words..

    could someone help me?

    Code:
    <script type="text/javascript">
    <!--
    function addtag(p_oObj, p_sText) {
    p_oObj.value = '[' + p_sText + ']' + p_oObj.value + '[/' + p_sText + ']';
    }
    //-->
    </script>
    
    <button onclick="addtag(document.getElementById('textarea'),'b')">B</button><br/>
    <textarea id="textarea" name="message" style="resize: vertical;" rows="12" cols="48"></textarea>
    Last edited by orhun; 12-17-2009 at 11:31 AM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    All your doing there is just wrapping the value of the textarea. You are no longer even using any method to get the selected text.

    Perhaps you should skip to the 'finished product' from the thread I mentioned:

    http://www.dynamicdrive.com/forums/s...3&postcount=33

    The code is still pretty basic and short, the various methods required for a cross browser solution for this are clearly shown. If you really want to learn about it though, I suggest you read the entire thread. Shouldn't take more than 20 minutes, if that.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •