diff --git a/src/utils/CBPayInstance.ts b/src/utils/CBPayInstance.ts index 15ec3b5..6a1b517 100644 --- a/src/utils/CBPayInstance.ts +++ b/src/utils/CBPayInstance.ts @@ -28,8 +28,10 @@ export interface CBPayInstanceType { export class CBPayInstance implements CBPayInstanceType { private pixel: CoinbasePixel; private options: InternalExperienceOptions; + private targetElement: HTMLElement | null; constructor(options: CBPayInstanceConstructorArguments) { + this.targetElement = null; this.options = options; this.pixel = new CoinbasePixel({ ...options, @@ -40,9 +42,9 @@ export class CBPayInstance implements CBPayInstanceType { }); if (options.target) { - const targetElement = document.querySelector(options.target); - if (targetElement) { - targetElement.addEventListener('click', this.open); + this.targetElement = document.querySelector(options.target); + if (this.targetElement) { + this.targetElement.addEventListener('click', this.open); } } } @@ -84,6 +86,9 @@ export class CBPayInstance implements CBPayInstanceType { }; public destroy = (): void => { + if (this.targetElement) { + this.targetElement.removeEventListener('click', this.open); + } this.pixel.destroy(); }; }