Why am I reluctant to learn/use jquery?

One for the web programming dopers.
For reasons I can’t quite fathom I shun jquery in favour of pure javascript. I think it’s because I don’t want to have to download a library to include in whatever I happen to be doing. I like to be able to write a pure javascript/php/mysql page.

I understand that by not using jquery and using javascript for things like ajax I am re-inventing the wheel. And at the learning stage it makes sense (learn how something works before you use something that does it for you).

Is it worthwhile to begin learning and using jquery if I want to progress as a website developer?

You’re not just reinventing the wheel, and AJAX is a relatively small part of what jQuery has to offer. JQuery provides a huge number of conveniences that reduce pure JS bulk by about 90%.

Of particular import is the ability to select and manipulate DOM nodes using CSS selectors. Say you want to change a style on every <td> element with the class “foobar.”



$( 'td.foobar' ).css( 'font-face', 'bold' );


That’s it. Do you see how ridiculously powerful that is? No tediously looping over crappy DOM API methods to find the elements and tweak them. Just select a bunch of crap and do stuff to it.

Event handling is another thing that makes jQuery superior. Suppose you want to have an event handler that triggers whenever someone clicks one of the above-mentioned table cells:



$( 'td.foobar' ).click( function() { alert( "You clicked a cell with this data: " + $(this).html() ) } );


You can easily associate arbitrary state data with any element(s) using the data API.



$( '#narf' ).data( 'takeovertheworld', 1 );


Say you want to loop over a number of row elements and hide them if the row number is above 12 and there’s a hide bit stored on them:



$( 'tr.foobar-row' ).each( function( i ) {
    var that = $(this); 
    if ( i > 12 && that.data( 'hidethis' ) === 1 ) { 
        that.hide();
    }
} );


And of course, the jQuery AJAX libraries abstract out all the tedium of making sure XMLHttpRequest stuff is handled correctly in every browser. It’s in use on tens of thousands of sites and is far better testes and bugfixed than anything you could come up with.

The coolest feature, though, is the live API, which allows you to bind an event handler to any group of elements matching a selector, and will then automatically bind that same handler to any newly-created elements that also match the selector.

Another example: creating new DOM elements. The DOM createElement API blows goats. Wouldn’t you rather be able to say something like



$( '#biglist' ).append( $( '<li>' ).html( 'a list item!' ) );


I thing jQuery has a high upfront learning issue, because the syntax is just butt stupid. Yeah, I get it that $( and such is easy to type, but it’s hell to try to read until you get a fairly high level of expertise. The stupid stuff looks like hieroglyphics.

That said, I agree with friedo - once you do take the time to learn it, it’s great. WAY nicer than vanilla Javascript. I just wish it had been written by someone who had code readability in mind.

I think it’s fairly readable. Really, the only weird thing is an object named $. And it has methods that return itself, which is unusual in Javascript but hardly a new idea.

Part of the difficulty of learning jQuery is you need to already be familiar with a lot of advanced Javascript features, like how Prototype-based OO work and how JS implements function objects, anonymous functions and closures. Once you get that, picking up jQuery is pretty easy.

ETA: Also, obviously it helps if you already know the basics of how CSS selectors work. $( ‘#narf ul li a.foo’ ) makes a lot more sense if you know that refers to a <a> tag with class foo inside a <li> inside a <ul> inside some element with id ‘narf’.

I’ll begin learning it and see how I get on. Thanks for the replies guys :slight_smile:

I think this sentence pretty much demonstrates my complaint about jQuery. :smiley:

Well that’s really a complaint about CSS, then. CSS selectors do take a bit of getting used to.

I think it’s both together. Don’t get me wrong - I do fine with them - but every time I look at a jQuery function I’m like “WTF? Can it get any more cryptic?!?”