Message from JavaScript discussions

May 2019

β€” Hi πŸŽ‰

β€” 

I'm trying to auto-trigger one event (call a function), but facing problems..


const n = document.createElement("button");
n.addEventListener("click", B)
B(); // I added this line in order to auto-trigger the function instead of clic, but error raises

...

function B(e) {
e.preventDefault(), e.stopImmediatePropagation(), e.isTrusted && !g && (m({
working: !0
}), async function(e) {
...
}

...

 new MutationObserver(h).observe(document.body, {
childList: !0,
subtree: !0
}), h()

❌
Uncaught TypeError: Cannot read property 'preventDefault' of undefined
at B (script.js:3606)
at MutationObserver.h (script.js:3241)

β€” Your function B requires a event parameter to work. But if you don’t use the event object in the important part of your function you can simply use an if(e) to conditionally execute those lines that require β€œe” to be defined.

Message permanent page

β€” Thanks Christian! Will try removing event and tweaking a bit that func 🀞🏻

β€” πŸŽ‰

β€” Uhm looks like B() depends a lot of e

β€” Oh

β€” You can try to call it in other event trigger like document load event

β€” πŸ‘ thanks Chris, will try now

β€” Ok, write here if you need any help

β€” πŸ™πŸ»πŸ˜Š

β€” No success