News for hackers. Stuff that matters.
Re:You might not need jQuery (2014) by peanut_worm on Wednesday January 13, 2021 @11:49PM (#25772170)
This is all very old javascript to be fair, the modern equivalents are as terse as jquery these days.
Re:You might not need jQuery (2014) by jamiesonbecker on Thursday January 14, 2021 @12:02AM (#25772290)
Somewhat true, but this was also old jQuery :)

Even the old jQuery seems easier to read than the modern ES6 equivalent (IMO).

jQuery:

    $(".classname").addClass("darktheme")
ES6:

    document.querySelector(".classname").classList.add("darktheme")
Re:You might not need jQuery (2014) by ryanbrunner on Thursday January 14, 2021 @06:21AM (#25775044)
Maybe a nit, but these aren't equivalent. The jQuery version will apply it to all elements that match your selector, where the ES6 version will only apply to the first matching element.

Equivalent code in ES6 would be (maybe there's a terser way but this is what I'd do at first glance):

    [...document.querySelectorAll('.className')].forEach(el => el.classList.add('darktheme'));
Not a whole lot extra in terms of actual code, but I'd argue it's not super intuitive (having to cast a NodeList to an array is a pretty big beginner footgun), and as you get into more complex scenarios, the jQuery architectural pattern of operating against collections of nodes really shines.
Re:You might not need jQuery (2014) by ratww on Thursday January 14, 2021 @06:44AM (#25775230)
You can use forEach without the splat:

    document.querySelectorAll(".className").forEach(el => el.classList.add('newClass'));
Re:You might not need jQuery (2014) by jamiesonbecker on Thursday January 14, 2021 @11:12AM (#25778091)
Still doesn't seem like much of a win compared to the jQuery :)

    $(".classname").addClass("darktheme")
Re:You might not need jQuery (2014) by peanut_worm on Thursday January 14, 2021 @08:37AM (#25776100)
NodeLists should have a forEach method as far as I know
Re:You might not need jQuery (2014) by arp242 on Thursday January 14, 2021 @12:53AM (#25772634)
There are quite a few more examples of this:

    $('.class').remove()
    $('.class').after(...)
vs:

    var e = document.querySelector('.class')
    e.parentNode.removeChild(e)

    document.querySelector('.class').insertAdjacentElement('afterend', ...)
Re:You might not need jQuery (2014) by realityking on Thursday January 14, 2021 @04:41AM (#25774238)
This is good example of how browser APIs have gotten better:

// Edge 12 document.querySelector('.class').remove();

// Edge 17 document.querySelector('.class').after(...);

Where jQuery shines - but also hides a lot of complexity- is when operating on an array of elements, e.g. if you want to remove all elements with a certain class.

Re:You might not need jQuery (2014) by jamiesonbecker on Thursday January 14, 2021 @01:09AM (#25772733)
This kind of code grows explosively in a moderately-sized project, and the sheer volume and verbosity makes it hard to debug. Not to mention requiring more tylenol ;)
Re:You might not need jQuery (2014) by jarek-foksa on Thursday January 14, 2021 @04:26AM (#25774107)
The modern DOM equivalent would look almost the same as the jQuery version:

  document.querySelector(".class").remove();
  document.querySelector(".class").after(...);
Re:You might not need jQuery (2014) by mejutoco on Thursday January 14, 2021 @08:08AM (#25775865)
Agree. Even before getting into things like: $(".classname").closest(".container").addClass("active”);

I do not get the hate over jquery, specially since frameworks like angular used (use?) to include a light version of it (sizzle or similar?) and methods like $httpParamSerializerJQLike, which does not scream of elegance.

I still remember when prototype.js was the default in rails, and jquery was so less overengineered and it just worked. It felt like magic.

Re:You might not need jQuery (2014) by 6510 on Thursday January 14, 2021 @10:50AM (#25777768)
The opinions mostly depend on what people know. The dvorak keyboard might be better?
Re:You might not need jQuery (2014) by asiando on Thursday January 14, 2021 @03:14AM (#25773579)
Yes but the former will silently fail on you. Good luck finding that!
Re:You might not need jQuery (2014) by mejutoco on Thursday January 14, 2021 @08:12AM (#25775895)
This is probably a feature. The non-jquery version will need a check for the elements existance to not blow up, which could the same be added to the jquery version for error reporting.

Plus, in this case, you will find out by seeing the dark theme is not active :p