Closed martyn0ff closed 2 months ago
How about adding the event listeners to all results of the query selector and not just the first one in your first example?
Oh, you're totally right! I don't work with JS much and I forgot that onXXX
properties only ever hold single event handler. However things would be different when we're dealing with addEventListener
- it would add the same event handler to the element in either of the modes once we switch to different mode (because JS Engine fires up once per mode) which would not be desirable.
I thought that there is a way for your plugin to detect adding event listeners to elements and ensuring that both modes will receive those handlers, but now that I think of it, is it even possible at all? Maybe it's at least worthwhile to warn about this event handling gotcha in JS Engine docs. Took me some time to figure out why my event handlers did not fire. :-)
In live preview mode, all event handlers that were put on elements by means of JS Engine don't work. The reason for this is that for some reason Obsidian duplicates elements - one is created for editor mode, and another for preview mode.
Steps to reproduce
```js-engine const ctr = document.getElementsByClassName("container")[0]; ctr.onmouseover = () => alert("boo"); ```