If you haven’t heard about jQuery, then you’ve most likely been hiding under the proverbial rock, from the proverbial reality, drinking the proverbial martini (the use of the word “proverbial” is as flexible as the jQuery framework).
One of jQuery’s greatest accomplishments is something that the W3C itself has not done: getting browsers to use CSS3 selectors. As many of you know, this is the equivalent of finding Atlantis. It is a great javascript tool, and the missing bridge between CSS and javascript development for front-end designers.
CSS, if you don’t know, is a specification that allows designers to grab or hook into elements (such as the <a></a> element) and then apply styles, color, layout, etc. CSS3 is the third iteration of this and has yet to be supported, but browsers are slowly adopting little rules and tidbits from the spec. Selectors, for our purposes, are the tools you use in .css documents (and .js or javascript documents) to grab elements on an HTML page and alter them. In CSS3, the amount of selectors from previous versions has increased. Where at one time we were limited to #navigation in css or getElementById('navigation') in javascript to refer <ul id="navigation">, we can now use a multitude of selectors based on various values. We then can apply our styles through javascript, or do other great alterations-- thanks to jQuery.
To setup and install jQuery, as well as get used to its lexicon, might I suggest this page: Getting Started with jQuery. This tutorial gives you a basic understanding of how jQuery works; but what I really want you to imbibe from this particular tome is the $() object. It is the universal basin from which our explanation shall thus flow. You plug a selector into the $() object (like this $('#navigation')) and it grabs it like those hands in that dungeon in the first Zelda. You know the one -- you spent hours playing it. You now have hooked an element to modify, move around, etc.
The $() object is what allows us to grab a certain element or a group of elements, much like you would with CSS. #navigation in CSS becomes $('#navigation') in jQuery. Similarly, the class .navigation_links becomes $('li.navigation_links'). It is, like CSS and a district attorney with something to prove, case-sensitive. The only significant difference, of course, is the $()> object that wraps the selector.
This is all well and good, but what’s really exciting about jQuery is all the fun stuff that this guy wrote about in this awesome book. It is progressive-enhancement with immediate results. What more could us iPhone swinging, twitter-using fiends enjoy more then instantaneous results? Nothing, that’s what. We can build Andy Clarke’s transcendent CSS3 world right now.
For this particular article, I’m going to focus on the “attributes” selectors, which are targeted towards the attributes in the CSS3 specification that target elements with the specified attributes and values. For example, in <a href=”foo”></a> href is an attribute, foo is its value.
Beginnings
So let’s talk about some basic attribute selectors (in digg-friendly list form):
$('[attribute]'): This is the basic attribute selector. The W3C states that this should select an element or elements with this attribute. For example, $(‘[href]’) would select all elements on the page (e.g.,<a>elements) with “href” links. It’s like Shake N’ Bake baby!$('[attribute=value]'): A little more definition and tone comes in through this selector. We can use the<a>element again. For example, use$('[href=http://www.foxnews.com]')to select all<a>elements with a link to foxnews, the only reputable news source in America (of course).
More advanced attribute selectors, or, the badass stuff.
So those are all well and good, and are especially helpful for outside links, but let’s talk about the much cooler, more advanced, impress-your-friends-and-mom (hi!) selectors.
$('[attribute!=value]'): This is a simple operation. Let’s say all our<a>elements have href attribute values of foxnews, except one. To select the one without a value of foxnews, we just put$('[href!="http://www.foxnews.com"]'). This reads as such: Please grab the element (in our theoretical<a>) with an href attribute that does not have a value of foxnews.$('[attribute^=value]')or$('[attribute$=value]'): These two are paired together because they do similar tasks. The ‘=’ between attribute and value means to grab the element whose attribute’s value begins with something (e.g.$('[href^="http"]')will grab all external links). The $= value will grab elements whose attribute value ends with some (e.g.$('[href$=".com"]')will also grab similar links. This requires a certain amount of specificity, whereas http://www.foxnews.com will be selected but http://www.foxnews.com/ will not be selected because it ends with the character "/" (and not just ".com").$('[attribute*=value]')is the most dangerous and also the most powerful. This selector will grab all attributes that contain some form of the value. For example, if you grab$('[href*="fox"]'), you’ll get all elements that link to fox news. A word of strong warning: this will also grab your element with a link to firefox.com because it contains the three letter combination “fox”. It's powerful because it looks between the lines for the phrases or values you’re searching for, but you must be specific in either your HTML or your “value” in order to get results you desire.
Playtime!
Play around: Load up firefox, load up firebug (an extension available for Firefox 2 at http://www.getfirebug.com or for Firefox 3 users go here.), enable the console* and go to www.jquery.com. Try this: $('[href]').hide(); You just hid all the links!
Now try this: $('[id="jq-wrapper"]').hide(); OH MY GOD! YOU BROKE F@!$&%G JQUERY’S SITE! Well… at least on your end. Trust me, it’s fine… for now. The <a> tag provides a great element to play with, but you can select attributes of other elements, too! Try grabbing title attributes on images, or class=value pairs.
How about some CSS alteration? $('[id="jq-wrapper"]').css("background-color", "green") will change the color of a background, based on the rules of CSS you already know. You can try other ones such as .css("float", "left") or .css("text-decoration", "underline") and apply it to any element on the page.
There are, of course, more advanced CSS3 selectors available on jQuery’s documentation. If you’re feeling exploratory, I suggest you try them out and even comment on your findings. However, you’ll really have fun when you start using those “grabbed” elements for effects with a ‘period’ or ‘dot notation’ to start manipulating the jQuery site (e.g. $('[id="jq-wrapper"]').hide();). Try out cool effects like .fadeOut() and then .fadeIn() to bring it back. You can find all those cool effects documented here.
That’s it for CSS3 attribute selectors with jQuery. I suggest you just try playing around with the jQuery site and Firebug to get the hang of it. It’s really basic, and quickly rewarding. It’ll give you an idea of how jQuery works with CSS3, and will prepare you for all the cool CSS3 stuff you’ll be able to do with stylesheets in the future.
*To learn about the console and do this stuff, go here: http://getfirebug.com/cl.html

Hey, Kenny—nice intro article to jQuery! Just wanted to point out that attribute selectors are, in fact, in the CSS2 specification.
Doesn’t change the thrust of your article, but thought I’d at least raise that point.
Hey Ethan,
Thanks for your shout out! The first two types of attribute selectors I discuss are CSS2, yes, but the last three are in fact only in the CSS3 Specification. I was wondering if I should include that tidbit, and of course, someone called me on it. But, I left it out, because they will work in CSS3!
Cheers, Kenny
Great article. Just wanted to let you know that I had some trouble following your examples using firebug. First, I was getting a “Security Manager vetoed action” error. I needed to upgrade firebug to the latest version (even asking Firefox to “find updates” didn’t work; had to go to the website). Then I copied and pasted your examples into the console, but it gave me “SyntaxError: illegal character”. Somewhere along the line, the straight quotes in your code were translated to curvy quotes. When I straightened them back out, the commands worked as advertised.
@Joel
Thanks for bringing it up. I’m going to see what I can do about changing them in the post. We don’t blog a lot with inline code samples here, so hopefully this is a rare case. There’s some post-processing of the articles “code” after we submit it, so I’ll correct that.
If you’re using Firefox 3 then you need to go to https://addons.mozilla.org/en-US/firefox/search?q=Firebug&cat=all I will correct my article to reflect that.
Thanks! Kenny
UPDATE!
I fixed the entry so that people can copy/paste. There was a problem with curly quotes :( Now fixed. If you guys find any other errors let me know.
Cheers, Kenny
Nice one Kenny, thanks for the shout out.
Hi, this is awesome. I just have a question, in the badass stuf, part 2.
You have this line: (e.g. $(‘[href=”http”]’)
Doesn’t it have to be (e.g. $(‘[href^=”http”]’) with the ^ between href and = ?
Grtz, Sibran
@Sibran
It does, and it’s fixed… what a whopper of a first post here at Blue Flavor :)
Thanks! Kenny
Did anyone has experienced malfunctioning of jQuery in Firefox 3. When I tried to find element in XML doc like following $(xml).find(‘element_name’) it didn’t work.
Very nice info,
thank you