# How to add confirmation pop up before deleting any annotation?

**URL:** <https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291>\
**Category:** Technical Support\
**Tags:** pdfjs-express\
**Created:** [January 12, 2022, 9:18am UTC](https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291 "2022-01-12T09:18:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arman](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@arman](https://pdfjs.community/u/arman)\
**Post date:** [January 12, 2022, 9:18am UTC](https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291/1 "2022-01-12T09:18:03Z")

</div>

**Which product are you using?**  
PDF.js Express Plus

**PDF.js Express Version**  
8.0.1

**Detailed description of issue**  
I want to add a confirmation popup before deleting any annotation. I already added my own popup modal but after clicking the cancel button pdfjs express delete my annotation. How to stop delete annotation if the user clicks the cancel button from the popup modal.

**Expected behavior**  
 ![Markdown](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/c2d54731694974669fe9c99b440ced1e31daffce.png)

After clicking the cancel button it shouldn’t delete the annotation.

**Does your issue happen with every document, or just one?**  
Yes, for every document.

**Link to document**

> **[195293122.pdf](https://core.ac.uk/download/195293122.pdf)**
>
> 84.29 KB

**Code snippet**

```javascript
const { annotationManager } = instance.Core;

  annotationManager.addEventListener('annotationChanged', async (annots, action) => {
    if(action === 'delete') {
      //show my pop up modal and wait for user response
       if(window.confirm()){
          // store XFDF in DB
       } else {
          //this function will stop here and return but it delete my annoattion
          return
       }
    }
    }
  })

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/ffbcd25c7d423db8d5acd5db201c272f4db306dd.png) [@system](https://pdfjs.community/u/system)\
**Post date:** [January 12, 2022, 9:18am UTC](https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291/3 "2022-01-12T09:18:08Z")

</div>

**Hello, I’m Ron, an automated tech support bot 🤖**

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:
- [Annotation permissions - Default behavior](https://pdfjs.express/documentation/annotation/annotation-permissions#default-behavior)
- [Set up server for real-time collaboration](https://pdfjs.express/documentation/collaboration/realtime-collaboration-server)
- [Adding your license key - PDF.js Express Plus License Keys](https://pdfjs.express/documentation/get-started/add-license-key#pdf.js-express-plus-license-keys-<span-class='plus'></span>)

APIs:
- [Adding a download button](https://pdfjs.express/api/utils/tutorial-Adding%20a%20download%20button.html#__anchor)
- [Core.DocumentViewer - setInternalAnnotationsTransform(handler)](https://pdfjs.express/api/Core.DocumentViewer.html#setInternalAnnotationsTransform__anchor)
- [Core](https://pdfjs.express/api/Core.html#__anchor)

Forums:
- [How to create custom annotation comment with id?](https://pdfjs.community/t/how-to-create-custom-annotation-comment-with-id/1240/1)
- [Is it possible to remove annotations of a certain page?](https://pdfjs.community/t/is-it-possible-to-remove-annotations-of-a-certain-page/1206/1)
- [Enable annotations to be moved using arrow keys](https://pdfjs.community/t/enable-annotations-to-be-moved-using-arrow-keys/602/1)

---

<div class="post-metadata">

**Author:** ![arman](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@arman](https://pdfjs.community/u/arman)\
**Post date:** [January 20, 2022, 8:17am UTC](https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291/4 "2022-01-20T08:17:56Z")

</div>

@Logan are you there?

---

<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:** [January 20, 2022, 5:19pm UTC](https://pdfjs.community/t/how-to-add-confirmation-pop-up-before-deleting-any-annotation/1291/5 "2022-01-20T17:19:09Z")

</div>

Hey!

Sorry - this slipped through the cracks somehow.

This one was a bit tricky. Basically the trick is to redraw the annotation after its deleted, pop up your modal, and if they confirm then re-delete the annotation. There are some timing issues we ran into when trying to integrate so we had to rely on `setTimeout` a bit to make this work. We will fix these timing issues in the next release - after that you should be able to delete the setTimeouts.

```javascript
  annotationManager.addEventListener('annotationChanged', async (annots, action, event) => {
    if(action === 'delete') {

      // If the source of this event is "confirm-delete" then we can break early to prevent infinite loop.
      if(event.source === 'confirm-delete') return;

      // Redraw the annotation for now
      setTimeout(() => {
        annotationManager.addAnnotations(annots);
        annotationManager.drawAnnotationsFromList(annots);
      }, 40)
    
      setTimeout(() => {

        //show pop up modal and wait for user response
       if(window.confirm('Delete?')){ 
          // Delete the annotation again
          // Set the source to something that we can catch later
          annotationManager.deleteAnnotation(annots[0], { source: 'confirm-delete' });
        } else {
            return
        }
      }, 100)
      
    }
  })

```

Thanks,  
Logan
