fix: initialize DOM elements only when executing methods/in pages that

contain those elements

SUMMARY
    Trying to grab elements globally in a script results in it trying to
    initialize in all pages. When the element is absent, the script
    fails and JavaScript crashes.
This commit is contained in:
realaravinth
2022-07-23 00:48:15 +05:30
parent b12c30e956
commit b6afe79a81
3 changed files with 20 additions and 12 deletions

View File

@@ -25,10 +25,10 @@ import { validate, FORM } from "../../add/novice/ts/form";
const SUBMIT_BTN = <HTMLButtonElement>(
document.querySelector(".sitekey-form__submit")
);
const key = SUBMIT_BTN.dataset.sitekey;
const submit = async (e: Event) => {
e.preventDefault();
const key = SUBMIT_BTN.dataset.sitekey;
const formUrl = getFormUrl(FORM);
const payload = {
pattern: validate(e),