Hacker News new | ask | show | jobs
by TekMol 2945 days ago
When I see this:

    var formname = $(this).find('.formname');
    formname.empty();
    formname.append(n_input);
I think why not just use plain Javascript and do this:

    document.querySelector('.formname').innerHTML=n_input;
Coders that are attached to libraries and frameworks always seem so afraid to use the tech already available. That they don't even look at how the basic solution would look like.
5 comments

Apples to apples -

    $(".formname").html(n_input);
The jQuery solution is shorter, and it works in browsers without querySelector (old IE) and without innerHTML (which is missing/readonly for some element types in old IE).
Sorry for being terribly pedantic, but the "vanilla" version just does not do what the jQuery code does:

- `this` might not be document

- find might return multiple elements, not just one

- if nothing matches, jQuery does not throw unlike the vanilla version

I do understand it's just an example, but if you'd consider all the implicit cases the jQuery example covers, you'd end up with a lot more than a single line. I see your point, but using off examples like this don't really sell it too well.

assuming "this" is an element... `this.querySelector` if you're intending to match multiples...

    Array.from(document.querySelectorAll(...)).forEach(x => {...})
It's not *that hard... though forEach should be on your dom collection returned, if Array.from exists, but I started shimming Array.from long before either were standard.
These seem to be unintended side effects. From the example it seems the vanilla versions behaviour is what the author wants.
Today yes. Good luck with that back in the Internet Explorer 6 days. Cross browser compatibility is what motivated jquery in the first place, today it has played out its role as a cross browser DOM manipulation library.

Modern libraries such as React and Vue however play a totally different role. They introduce proper architecture to web development such as separating the view from your business logic with data binding, they can not be compared with jquery which was just a nicer way of doing the same old manual DOM manipulation.

Real life example: https://caniuse.com/#feat=urlsearchparams if you need to support IE11 then you can't use the URLSearchParams object. Your best bet is still a jQuery library.
I'm currently refactoring a jQuery frontend to native Web. The verbose naming is really unfortunate, jQuery wins here.