Capturing a Text Selection : Text Selection : HTML : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP
JavaScript DHTML Home »  HTML   » [  Text Selection  ]   
 



Capturing a Text Selection

Please note that some example is only working under IE or Firefox.


/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428
*/
<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"!= -&& 
    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>
Congress shall make no law respecting an establishment of religion, or prohibiting the 
free exercise thereof; or abridging the freedom of speech, or of the press; or
 the right of the people peaceably to assemble, and to petition the government
 for a redress of grievances.
</P>
</HR>
<FORM>
<TEXTAREA NAME="selectedText" ROWS=COLS=40 WRAP="virtual"></TEXTAREA>
</FORM>
</BODY>
</HTML>

           
       
Related examples in the same category
1.  Using the TextEdit Property
2.  Using the elementFromPoint() Method
3.  Get selected elememnt
4.  Get selecte text in one paragraph








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.