Why jQuery is still needed

Updated: 20th January 2023
Tags: javascript jquery

TLDR: A lot of old libraries that require jquery are much better than new without jquery dependency.

I was working on rewriting a website that uses jquery and some jquery legacy libraries. Hear me out, most new libraries that have no jquery dependencies are worse than with jquery.

I don’t know why but, for example, bootstrap 5 collapse without jquery is lagging like sh*t.

For example, we will have a button that shows all collapsed elements and other button to hide all. We have 100 collapsed elements.

//bootstrap4 jquery sample
var multiCollapsedElements = $('.js-multi-collapse');

function showAllCollapsed() {
    multiCollapsedElements.collapse('show');
}
function hideAllCollapsed() {
    multiCollapsedElements.collapse('hide');
}
//bootstrap5 pure js sample
const collapseElementList = document.querySelectorAll('.js-multi-collapse');
//next line will eat all your cpu and will take 4-5 seconds
const collapseList = [...collapseElementList].map(collapseEl => bootstrap.Collapse.getOrCreateInstance(collapseEl, {toggle: false}));


function showAllCollapsed() {
    collapseList.forEach(el => el.show());
}
function hideAllCollapsed() {
    collapseList.forEach(el => el.hide());
}

Yeah, yeah we can fix this without fancy collapse animation, which I did.

//bootstrap5 pure js sample, fixed
const collapseElementList = document.querySelectorAll('.js-multi-collapse');


function showAllCollapsed() {
    collapseElementList.forEach(el => el.classList.add('show'));
}
function hideAllCollapsed() {
    collapseElementList.forEach(el => el.classList.remove('show'));
}

And this is only one simple example, where changes are minimal, and I’m ok with fixed code.

There are tons of libraries that require jquery (lightboxes, typehead, select2) that doesn’t have a good alternative. Or have new version that is worse than original jquerys one (lots of lightboxes, fancyboxes).

Authors rush to make jquery-less libraries, but very often new jquery-less libraries are worse. I’m using jquery just to have one or two good libraries.
And I did waste few days to come to this conclusion.