Sometimes you have to break a bone for it to set properly

One of the recent changes applied to improve lighthouse scores is using the 'defer' option when loading JavaScript files. Loadtimes are greatly improved, but at the cost of breaking everything.

Time to fix things
1: Edit a page - the code that handles the form submission is broken - let's start there.
Ok - that's done. One of the main problems I've hit with 'defer', is that you can't use $(document).ready any more with inline scripts. Wrapping $(document).ready in an addEventListener('load') function prevents the error, but if you need to reload a JS file into a dynamically loaded content - the addEventListener wrapper doesn't work.

I expect there's a more elegant solution, but the work around I've come up with is to create copies of the JS files without the addEventListener wrapper and use those for dynamically included content. It's safe to assume that JQuery is already loaded when the dynamics content is fetched as it would be JQuery doing the fetching.

I'm not a big fan of duplicating code - but it's the only reliable option I can find for the time being. Time for some more fixes.

2. More instance of the same problem - check that the 'non deferred' version of the relevant script is being called when editing an element.

Ok. The main publishing functions seem to be working again - 'add' and 'edit'. Updating format options is also back, and improved thanks to .replaceWith(). For example, in order to change a heading from H2 to H4 or similar, used to wrap the tag in a div that had an id which meant the contents of the parent div could easily be changed from an H2 to and H4. It worked, but wrapping things in divs can mess up styling and layout. replaceWith means I can simply give the H2 the id without wrapping it in a div and simply replace it with an H4 with the same id. I just wish I'd spotted this function before. It's been there since JQuery 1.2 2007. One of these days I'm going read the manual.

Next bugs: a new element is disappearing - which means correct id but wrong content call. Also, the selected dropdown items are not matching the relevant text sector.
The second issue has been addressed. Again, I suspect there's abetter way, but operation "Get things working again" needs to be completed quickly.

Will this new paragraph of text appear as it should after I hit the button?

Nope. But this one did. Loads more to do, but time to park things for today. Next bug: update the display of the updated thing -arg! Ok - sot that's mainly fixed. Time to stop.