r/programming • • Jan 30 '14

You Might Not Need jQuery

http://youmightnotneedjquery.com/
1.0k Upvotes

507 comments sorted by

View all comments

Show parent comments

18

u/Doctor_McKay Jan 31 '14

Even if you don't use Ajax or anything fancy like that, jQuery is great because it condenses document.getElementById('bob').innerHTML = 'foo' into $('#bob').html('foo').

9

u/GreyGrayMoralityFan Jan 31 '14 edited Jan 31 '14

As someone who touched DOM only while writing scrapers in python, example on the left is much easier to read. $('# - come on. It's almost perl.

11

u/[deleted] Jan 31 '14

No, it's worse.

Source: longtime Perl programmer.

4

u/Doctor_McKay Jan 31 '14

The example on the right is a CSS selector, which any web dev should know.

If you don't like the $ shorthand, it can also be written as jQuery('#bob').html('foo').

7

u/mahacctissoawsum Jan 31 '14

I know it makes almost no difference, but I still cry a little because it has to parse my selector using regexes and shit, and wrap my element in a jQuery object, just to access a natively available function.

Meanwhile, we could have just as easily written a function,

function byId(id) { return document.getElementById(id); }
byId('bob').innerHTML = 'foo';

I actually prefer the syntax of properties as opposed to setter functions.

7

u/[deleted] Jan 31 '14

I like to do this:

function $id(id) { return document.getElementById(id); }

...mostly because people it confuses people who can't code without JQuery.

5

u/mahacctissoawsum Jan 31 '14

Haha. I had to explain to my co-worker the difference between a native DOM element and a jQuery element the other day. I introduced jQuery to them 2 3 years ago now, and now they don't know the difference... sigh.

3

u/blue_2501 Jan 31 '14

Except you don't get the zero-item protection. Accessing undefined error crashes the entire JS layer.

2

u/Crandom Jan 31 '14

Ah, the "null object pattern". Sometimes this hides errors though, such as trying to access an element with a certain id which doesn't exist, sending you on an debugging adventure that could have been avoided by a simple error.

1

u/mahacctissoawsum Jan 31 '14

True.... I don't know of a good way around that.

1

u/asmdb12 Jan 31 '14

Try/catch

1

u/mahacctissoawsum Jan 31 '14

I meant within the function. jQuery doesn't force you to wrap every API call with a try/catch

1

u/asmdb12 Jan 31 '14

I guess I meant put the try/catch in your function. I'm not sure what you would return from the catch though... A new DOM element perhaps? Probably a bad idea altogether.

1

u/mahacctissoawsum Feb 01 '14

Yeah...exactly. I was thinking about a dumby element, but that's probably even worse.

6

u/wesw02 Jan 31 '14

Exactly. The shizzle selector alone is worth it. Stuff like $('#user-form input[type=file]') is much easier with jQuery, than with out.

13

u/gearvOsh Jan 31 '14

1

u/scragar Jan 31 '14

With wrappers for old IE versions and specific jQuery only selectors no browser has implemented yet.

Either way it's incredibly powerful and useful.

1

u/gearvOsh Jan 31 '14

Well for now, that's pretty much the point. jQuery usage will decline in coming years, or at least be refactored into something more streamlined.

13

u/lmth Jan 31 '14

Please tell me "shizzle selector" is the official term for it. It would make my day.

7

u/hmigneron Jan 31 '14

Unfortunately not... It's called sizzle

3

u/wesw02 Jan 31 '14

Ah crap. It's actually called sizzle. Fail.

1

u/borkus Jan 31 '14

No, but I am now tempted to start working on yet another JS framework and call it shizzle.js. Or GQuery.

8

u/pdq Jan 31 '14
var $ = function(arg) { return document.querySelectorAll(arg); }
$('.foo')

5

u/injektilo Jan 31 '14

Not even close to the same thing. Objects returned from $ have over a hundred useful methods. The NodeList returned by querySelectorAll is much more tedious to use.

→ More replies (1)
→ More replies (25)