javascript html selection

Discussion in 'JavaScript' started by catapop, Nov 20, 2008.

  1. #1
    Hi. I am trying to create a function in js that returns the html code for the selected text in the page. I've searched google and i found only simple text selection.
    here is the code of what i've found

    <HTML>
    <HEAD>
    <TITLE>Getting Selected Text</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    var isNav4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4)
    var isNav4Min = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 4)
    var isIE4Min = (navigator.appName.indexOf("Microsoft") != -1 && parseInt(navigator.appVersion) >= 4)
    function showSelection() {
        if (isNav4Min) {
            document.forms[0].selectedText.value = document.getSelection()
        } else if (isIE4Min) {
            if (document.selection) {
                document.forms[0].selectedText.value =
                    document.selection.createRange().text
                event.cancelBubble = true
            }
        }
    }
    if (isNav4) {
        document.captureEvents(Event.MOUSEUP)
    }
    document.onmouseup = showSelection
    </SCRIPT>
    </HEAD>
    <BODY>
    <H1>Getting Selected Text</H1>
    <HR>
    <P>Select some text and see how JavaScript can capture the selection:</P>
    <H2>ARTICLE I</H2>
    <P>
    text text text text text text text text text text text text text 
    </P>
    </HR>
    <FORM>
    <TEXTAREA NAME="selectedText" ROWS=3 COLS=40 WRAP="virtual"></TEXTAREA>
    </FORM>
    </BODY>
    </HTML>
    Code (markup):
    this example only returns the selected text not the html tags. I'm interested in a function that returns html code.
    Any help will be useful . Thanks you
     
    catapop, Nov 20, 2008 IP
  2. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Try using innerHTML. Maybe it will work.
     
    cc2365, Nov 21, 2008 IP
  3. catapop

    catapop Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no. innerHTML shows info from a tag. anyway i found the problem but I don't think it's possible. the browser wil add automatecly the missing tags in your selected text
     
    catapop, Nov 25, 2008 IP