• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Selecting Text

You can use the select method to select text in a web browser using text ranges. Here’s an example that shows how that works. When you click the button in this web page, the code creates a text range, moves that text range to encompass the text in a <SPAN> element, and selects that text:

(Listing 11-03.html on the web site)

<HTML> 
    <HEAD> 
        <TITLE> 
            Selecting Text 
        </TITLE> 

        <SCRIPT LANGAUGE="JavaScript"> 
            <!--
            function selectRange() 
            { 
                range = document.body.createTextRange()
					range.moveToElementText(text1)
					range.select() 
            } 
             // --> 
         </SCRIPT> 
    </HEAD> 

    <BODY> 
        <H1> 
            Selecting Text 
        </H1> 

        <FORM> 
            <INPUT TYPE="BUTTON" VALUE="Click Me" ONCLICK="selectRange()"> 
        </FORM> 
        <BR> 
        <SPAN ID="text1">Click the button to select this text.</SPAN> 
    </BODY> 
</HTML> 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint