From 4a56bc765fd97dd38918c21010aff367bbd6fa43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 28 May 2024 12:13:52 +0200 Subject: [PATCH] fix: :bug: acceptCookie and denyCookie were trying to change domElements from cookie list event if not present --- src/ts/index.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ts/index.ts b/src/ts/index.ts index 11e8290..4b346b2 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -281,10 +281,13 @@ export default class CookieNotice { acceptCookie(cookieName: string) { const cookieOptions = this.getCookie(cookieName); this.setCookieStatus(cookieName, CookieStatus.true, cookieOptions); - const cnSwitch = document.getElementById(`${cookieName}-switch`) as HTMLInputElement; - const cnSwitchInput = cnSwitch!.querySelector("input") as HTMLInputElement; - cnSwitchInput!.checked = true; - cnSwitch!.ariaChecked = "true"; + const cnSwitch = document.getElementById(`${cookieName}-switch`) as HTMLInputElement | null; + const cnSwitchInput = cnSwitch?.querySelector("input") as HTMLInputElement | null; + if (!cnSwitch || !cnSwitchInput) { + return; + } + cnSwitchInput.checked = true; + cnSwitch.ariaChecked = "true"; } /** @@ -301,10 +304,13 @@ export default class CookieNotice { denyCookie(cookieName: string) { const cookieOptions = this.getCookie(cookieName); this.setCookieStatus(cookieName, CookieStatus.false, cookieOptions); - const cnSwitch = document.getElementById(`${cookieName}-switch`) as HTMLInputElement; - const cnSwitchInput = cnSwitch!.querySelector("input") as HTMLInputElement; - cnSwitchInput!.checked = false; - cnSwitch!.ariaChecked = "false"; + const cnSwitch = document.getElementById(`${cookieName}-switch`) as HTMLInputElement | null; + const cnSwitchInput = cnSwitch?.querySelector("input") as HTMLInputElement | null; + if (!cnSwitch || !cnSwitchInput) { + return; + } + cnSwitchInput.checked = false; + cnSwitch.ariaChecked = "false"; } /**