www.webdeveloper.com
Results 1 to 2 of 2

Thread: getSelection() - htmlText for FireFox question

  1. #1
    Join Date
    May 2006
    Posts
    31

    How to get HTML source when highlight text? My code works on IE - not FireFox - Help

    Hello!

    The javascript code below works like this:

    When you run it on I.E, I use mouse to highlight the bold text "Highlight Me Now" and click button "Test" - I see the alert box "<B>Highlight Me Now</B>" .

    But in FireFox browser, it shows the alert "Highlight Me Now" without tag <B></B>. How to make FireFox grab the HTML tag <B></B> like the way IE browser did?

    Here is my code
    HTML Code:
    <script>
    function GETSELECTION(){
        if(document.all){
            var selection = document.selection;
            var newRng = selection.createRange();
            newRng.select();
            html_selection=newRng.htmlText;
            alert(html_selection);
        }
        else{
            var html_selection = document.getSelection();
            alert(html_selection);
        }
    }
    </script>
    <p><b>Highlight Me Now</b></p>
    <input onclick="GETSELECTION();" type="button" value="Test">
    Thanks
    Last edited by kevin31; 02-19-2007 at 11:54 AM.

  2. #2
    Join Date
    Jul 2007
    Posts
    3
    Hey Kevin,

    I'm experiencing the same frustrations, except with my code, I'm not outputting the result of the getSelection to an alert box. The reason you're getting only the text in FF is explained here

    Simply put, alerts are only meant to receive Straight text.. :-\

    Also, for firefox you should probably use window.getSelection instead of document.getSelection as that method is deprecated.

    Although I don't think changing it is crucial to your script...it will cause a warning in your Javascript console.
    Last edited by datdesignguy; 07-14-2007 at 11:27 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles