jQuery Is Awesome. Yet Again.

I know that this probably isn’t news to any of you who care about such things and follow the world of web development even a little… it’s not even news to me, really – I’ve been an advocate of this particular programming library for a while now. But today in particular, I just felt so enamoured by the elegance of the jQuery Javascript Framework that I had to tell you about it.

This line of code:

$('.alpha').not(':has(.beta:visible)').hide();

Hides all elements with the class “alpha” which contain no visible elements with the class “beta” (i.e. if it contains any visible elements of class “beta”, the “alpha” is not hidden).

And it’s just beautiful. Just to compare how elegant it is to something else, here’s the equivalent code in Prototype, another popular Javascript framework, which in itself still shortens the amount of code that this would take in plain-old vanilla Javascript:

$$('.alpha').each(function(element){
var has_visible_beta = false;

element.childElements().each(function(inner_element){
if (
inner_element.hasClassName('beta') && inner_element.visible()) has_visible_beta = true;
});
if (
has_visible_beta) element.hide();
});

(okay, that Prototype code could probably be a hair simpler, but you get my point)

Wow.

0 comments

    Reply here

    Your email address will not be published. Required fields are marked *

    Reply on your own site

    Reply by email

    I'd love to hear what you think. Send an email to b1634@danq.me; be sure to let me know if you're happy for your comment to appear on the Web!