I run this code on Duolingo exercises (you need to register to duolingo.com to go through one of these).
This code works only twice, and then no longer works — i.e, the function call doesn’t re run the code endless times.
On the other hand if I do location.reload(true) the code will work again for 2 times.
What is the difference between location.reload(true) and the function call in the sense in location.reload(true) the code will work again but in the function call it won’t? What’s changed with the former that isn’t changed with the latter?
Hi, why do you need to keep rerunning this code, recreating DOM elements and reattaching the event listener? What are you trying to accomplish? Surely it would be sufficient to run the code once and be done.
You are right. It will do no good and I did that by mistake. In my original code which I’ll paste in a moment the addEventListener was called just once, but it didn’t work more than once, so I thought, mistakenly, that there might be some framework/library bias and that if I’ll call it each time anew it will work.
Here’s what I’ve learned from it in chronological order:
According to my understanding of the author (Brock Adams) in the comments section, z before Event in zEvent could help distinguish the stream “Event” from other contexts it might have, and z is taken from the Hungarian notation convention in computer programming.
if (myInput) {} makes sure the code will keep running even if the element was not found and it’s always good to do so.
It’s important to keep clear from large automatic selectors like ._7q434._1qCW5._2fPEB._3_NyK._1Juqt._3WbPm and prefer giving a basic element type to target TEXTAREA, and using this elements attributes as in ('textarea[data-test]') to ensure targeting which is specific enough.
A basic reference to the event target (the event’s associated element) must first include a general reference to the type of that target (if (zEvent.target.nodeName === 'TEXTAREA'), and only then, a local, more specific reference (let myInput = document.querySelector ('textarea[data-test]')).
nodeName of an event target should usually be written all uppercased.
Note, I took down the // @run-at document-idle because by principle I prefer not to use userscrip agent shortcuts for JS aspects like DOMContentLoaded.
Meh! You’re dealing with an event object, so why not call it event? This convention is used all over the internet.
Kinda.It’s a good idea here, as you’re not in control of the page you are running the code against. I generally wouldn’t do this in code which manipulates HTML I had written.
Meh! You’re dealing with an event object, so why not call it event? This convention is used all over the internet.
Pullo, I personally agree with you and I assume that event is more common than zEvent.
In my local copy, I intent to call it just event or ev (as opposed to el or e for element).
I just noted my understanding of what the author wrote.
Kinda.It’s a good idea here, as you’re not in control of the page you are running the code against. I generally wouldn’t do this in code which manipulates HTML I had written.
I agree. I should wrote there “at least sometimes”.