# Handeling test selection programatically (caret manipulation)

**URL:** <https://pdfjs.community/t/handeling-test-selection-programatically-caret-manipulation/408>\
**Category:** Technical Support\
**Tags:** pdfjs-express\
**Created:** [December 6, 2020, 2:12pm UTC](https://pdfjs.community/t/handeling-test-selection-programatically-caret-manipulation/408 "2020-12-06T14:12:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cuzzea](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/cuzzea/32/209_2.png) [@cuzzea](https://pdfjs.community/u/cuzzea)\
**Post date:** [December 6, 2020, 2:12pm UTC](https://pdfjs.community/t/handeling-test-selection-programatically-caret-manipulation/408/1 "2020-12-06T14:12:38Z")

</div>

**PDF.js Express Version**  
“6.3.3” Evaluation

**Detailed description of issue**  
I am trying to work with text selection on pdf’s and I want to see if pdf.js express is a good fit for my project. To be more exact, I am trying to set a selection on a texteditor (either input or an annotation) to select a specific word, replace it and/or move the selection to a new word.

For the input[type=text] part I tried to get the html by using

```javascript
this.instance.docViewer.on('documentLoaded', () => {
        this.instance.docViewer.getAnnotationsLoadedPromise().then(() => {
                // setTimeout here
                this.instance.annotManager
                        .getFieldManager()
                        .forEachField((fields) => {
                                fields.widgets.forEach((field) => {
                                        if (field.element.type === 'input') {
...

```

but I found that some times it comes back as null, other times it comes back as the div that encloses the input, etc. Adding a setTimeout seems to fix this and it’s a bit more reliable, but I wanted to ask here if there is a `proper` way to achieve this.

In short I need to get access to the html input element (or textarea) so I can manipulate the selections with `getSelection()` and `ranges`.

Another issue that I couldn’t get around is how do I get access to the texteditor in the textTool annotation tool?

Any support would be apreciated.

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [December 8, 2020, 3:24pm UTC](https://pdfjs.community/t/handeling-test-selection-programatically-caret-manipulation/408/2 "2020-12-08T15:24:54Z")

</div>

Hi there!

Instead of `documentLoaded`, try using the [`annotationsLoaded`](https://pdfjs.express/api/CoreControls.DocumentViewer.html#event:annotationsLoaded__anchor) event. This will be fired after all the inputs are loaded.

Regarding your second question, I’m not to sure what you mean. Can you give a bit more detail about what you mean here?

Thanks!  
Logan
