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"; } /**