News for hackers. Stuff that matters.
Re:I loved jQuery, and still do (2019) by developeron29 on Tuesday June 29, 2021 @11:27AM (#27677234)
Yes Jquery does makes things easier than vanilla JS

Best being

$("#id") replacing document.getElementById("id");

Makes code look cleaner

Re:I loved jQuery, and still do (2019) by cptrp2101 on Tuesday June 29, 2021 @11:29AM (#27677273)
https://developer.mozilla.org/en-US/docs/Web/API/Document/qu...

Does the same as the $ query but native in every browser in IE9+ and gives you a native node. Also more performant than getElementBy<whatever> methods.

Re:I loved jQuery, and still do (2019) by Turing_Machine on Tuesday June 29, 2021 @12:05PM (#27677863)
document.querySelector(selector) is more typing and more visual clutter on the editor screen.

jQuery is elegant, concise, and was designed by a genius. The "modern" methods are ugly, verbose, and were designed by a committee.

Re:I loved jQuery, and still do (2019) by recursive on Tuesday June 29, 2021 @12:12PM (#27677950)
`const $ = document.querySelectorAll.bind(document);`
Re:I loved jQuery, and still do (2019) by rement on Tuesday June 29, 2021 @12:10PM (#27677931)
You can define your own `$` function. This way you can have the clean code without the entire jQuery library

    function $(arg) {
        if (arg.charAt(0) == "#") {
            // HTML spec does not support ids to start with numbers [0]
            // (you may not need this conditional on your website)
            return document.getElementById(arg.slice(1))
        }
        return document.querySelector(arg)
    }
Using this function you can select your comment with

    $('#27677234')
jQuery does add many extra features but if clean code is the only thing you are after there are other options.

[0] https://www.w3.org/TR/html4/types.html#type-id

Re:I loved jQuery, and still do (2019) by rement on Tuesday June 29, 2021 @12:22PM (#27678089)
The HN js actually does just this. The file isn't very long but has some great functions at the top

https://news.ycombinator.com/hn.js

Re:I loved jQuery, and still do (2019) by gunapologist99 on Tuesday June 29, 2021 @01:15PM (#27678844)
True, but jQuery does a lot more than just id selection with $!

You could just extend the function to detect '.' vs '#', and do a class selection as well. And then add all of the selectors, subselectors, etc. (similar to, but far more powerful than css3's selectors.) and if you go far enough, you reinvented zepto (but still a long way from jQuery)

(actually, since $ is basically synonymous with jQuery, it'd probably be better to choose a different function name. too bad you can't define it as #(id).)

Re:I loved jQuery, and still do (2019) by recursive on Tuesday June 29, 2021 @01:58PM (#27679454)
> True, but jQuery does a lot more than just id selection with $!

Yes. For a high degree of jquery-compatibility, you can use my library.

    const $ = document.querySelectorAll.bind(document);
Re:I loved jQuery, and still do (2019) by goldenkey on Tuesday June 29, 2021 @03:42PM (#27680782)
This comment made me spit my drink out. People forget about bind, it's nice to replace the old closure patterns.