jQuery - "getting" chains

I've been using jQuery for quite a while now. I really like its interface, often it feels very Perlish to me.

But in the past I've struggle when trying to process or access parts of a single element returned by a selector. Most of the time, I really want to grab it with an array index, e.g.
$('.special-sauce')[0].href
(just some rough example).

But that always felt wrong, against the jQuery way, like i was an outsider. Esp when its so natural do things like:
$('a.special-sauce').click( function() { // do something } );

I've tried to use the first() method to get a single element but that returns an array too. But I finally got how to use it today...

We are using jQuery Tools for overlays and some other actions. I wanted to retrieve the href attribute for the trigger object (the one that was clicked to open the overlay (aka lightbox). I tried the wrong feeling:
this.getTrigger()[0].href
and had the moment of clarity that said "that really means:
this.getTrigger().first().attr('href')
And now i get it (for now) :)

Since I want some part of the element like an attribute or value I can use the first method with either attr or val methods (or whatever part of the element I really want).

__END__

Comments

Popular posts from this blog

Changing Dancer::Plugin::Ajax's content type

BootstrapX clickover

2 ways to get SQLite to put dates into columns