As a follow up on jQuery performance, I tested two simple pages with the above code, and measured the page loading and function running time.
Page loading (ms)
JavaScript: 33, 35, 26, 37, 36, 31. Average 33
jQuery: 75, 67, 70, 67, 70, 76. Average 71
Run function
JavaScript: .29, .07, .14, .20, .14, .11 Average 0.16
jQuery: 1.65, 2.79, 1.90, 2.51, 3.29, 1.35 Average 2.25
With jQuery, loading the page is more than twice as slow, and running even this simple function is 14 times slower.
Now even though a few milliseconds isn’t much individually, what these numbers mean is that vanilla JavaScript achieves things 14 times faster, letting you do much more in the same time that it takes jQuery to do things once.
jQuery was useful about 20 years ago, but over the last 10 years the need for it has been declining to the point where there is less pain using vanilla JavaScript instead.
not sure i entirely agree on the ‘less pain’ part when it comes to all aspects of jquery vs javascript (the AJAX implementation still leaves much to be desired, as an example imo), but it’s certainly come a long way - mostly a case of javascript actually listening to what people use external frameworks for and incorporating the frameworks’ ideas, which is good.
If you’re going for a fully jQuery-esque translation, you would probably also add the event bind to the javascript, rather than using the html attribute association.
About ajax, have you done much exploring of the fetch API? This article gives a good breakdown of the old way to do Ajax, and the better way to use fetch.