document.getSelection()

JavaScript 1.2+ Nav4+ Syntax

document.getSelection()

Description

The getSelection() method returns the text that is selected within the HTML document.

Example

Listing 7.101 uses the getSelection() method to display all captured text in a text box. The result of selecting JavaScript is Great! from the text is shown in Figure 7.7.

Figure 7.7. Selecting a portion of text and displaying it in a text box.

Listing 7.101 Displaying the Selected Text
 <html> The following text area will display any text that you select within the Web page. Try selecting the phrase "JavaScript is Great!" with the mouse. <form name="Form1"> ...

Get Pure JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.