---
title: "Text selection events in PDF viewer | Nutrient"
canonical_url: "https://www.nutrient.io/guides/web/events/text-selection/"
md_url: "https://www.nutrient.io/guides/web/events/text-selection.md"
last_updated: "2026-05-14T21:57:26.952Z"
description: "Access and manipulate text selections using the Nutrient Web SDK’s cross-browser API. Retrieve selected text and lines efficiently within specified ranges."
---

Nutrient Web SDK comes with a reliable, cross-browser API to access text selection and retrieve both selected text and text lines located within a given selection range.

## Subscribing to TextSelectionChange events

Text selection updates are dispatched every time a text is selected or deselected. Subscribing to the `textSelection.change` event ensures a notification when the selection changes:

### ES6+

```js

instance.addEventListener("textSelection.change", textSelection => {
  if (textSelection) {
    textSelection.getText().then(text => {
      console.log(text);
    });
  } else {
    console.log("no text is selected");
  }
});

```

### JAVASCRIPT

```js

instance.addEventListener("textSelection.change", function(textSelection) {
  if (textSelection) {
    textSelection.getText().then(function(text) {
      console.log(text);
    });
  } else {
    console.log("no text is selected");
  }
});

```

Notice that when the text is deselected, the `textSelection` argument is `null`.

---

## Related pages

- [Subscribe or unsubscribe to API events](/guides/web/customizing-the-interface/observing-changes-with-events.md)
- [Manage annotations effectively with our API](/guides/web/events/annotation.md)
- [Events and notifications in our JavaScript viewer](/guides/web/events.md)
- [Manage bookmarks efficiently with event-driven updates](/guides/web/events/bookmarks.md)
- [Forms](/guides/web/events/forms.md)

