Prototype Gets Attribute Selectors
After releasing event:Selectors about 6 days ago the response was overwhelming. The question asked by most was if event:Selectors supported attribute selectors. My response a week ago was no, however, my response today is yes!
Thanks to Brian Donovan for the quick call to action in getting a patch into Prototype. Grab the latest Prototype to check it out. There is no need to update the event:Selectors script itself because this functionality was purely dependent on Prototype.
How they work
If we wanted to get all input elements whose type was submit we could use the = operator.
input[type="submit"] { color: #ccc; }
Brian also snuck in a non-standard != (not equal to) which is pretty useful.
a[href!="#"] { background: #c00; }
You can also throw in multiple attribute selectors if you’d like:
a[class~=external][href="#"] { font-style: italic; }
Check out the unit test for more examples and don’t forget to read up on attribute selectors over at W3C because some of them have quiet the arbitrary meaning.
Related articles
Sorry, comments are closed for this article.
Justin develops iPhone and Mac software over at Labrat Revenge by night and by day he is the Design Director for entp, the company behind Lighthouse, Warehouse, and Mephisto. You can check out his dated portfolio for a peek of what he's done.
-

Beautifully simple issue tracking and project management. With a beautiful Mac-like interface, email and subversion integration, Lighthouse is the perfect project management tool. We took the suck out of issue tracking.
-
Jan12
YouTube has just announced HouseHub and Senate Hub.
permalink -
Oct10
Best Practices For Cocoa and CocoaTouch—Invaluable tips from Cocoa developers.
permalink -
Sep25
Track House And Senate Votes On Twitter. I’ve put together a small script that parses vote data collected by Govtrack.us and posts to twitter.
permalink -
Jul28permalink
Protip: Color grep searches in terminal
alias grep='GREP_COLOR="1;37;41" LANG=C grep --color=auto'Stash this away in your Z Shell (~/.zshenv) or Bash environment (~/.bashrc) and set your preferred ANSI code.
-
Jun08
ActiveRecord Ported To Objective-C brought to us by Ninja Kitten
permalink -
May06
With the release of Opera’s DragonFly, IE 8’s Developer tools, Safari’s/Webkit’s Web Inspector and Drosera; and the Grand Daddy of them all, Firebug – we’ve now come full circle.
permalink -
Feb08permalink
DoubleClick and Define using Apple Dictionary
var selection; if(window.getSelection) selection = window.getSelection(); else if(document.selection) selection = document.selection.createRange(); document.observe("dblclick", function() { if(navigator.userAgent.include("Macintosh")) { location.href = "dict://" + selection; } });A quick (and probably dirty) Prototype-based hack allowing Mac users to get the definition of any word by double clicking it.
-
Feb08permalink
Object.extend(Date.prototype, { strftime: function(format) { var day = this.getUTCDay(), month = this.getUTCMonth(); var hours = this.getUTCHours(), minutes = this.getUTCMinutes(); function pad(num) { return num.toPaddedString(2); }; return format.gsub(/\%([aAbBcdDHiImMpSwyY])/, function(part) { switch(part[1]) { case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break; case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break; case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break; case 'B': return $w("January February March April May June July August September October November December")[month]; break; case 'c': return this.toString(); break; case 'd': return this.getUTCDate(); break; case 'D': return pad(this.getUTCDate()); break; case 'H': return pad(hours); break; case 'i': return (hours === 12 || hours === 0) ? 12 : (hours + 12) % 12; break; case 'I': return pad((hours === 12 || hours === 0) ? 12 : (hours + 12) % 12); break; case 'm': return pad(month + 1); break; case 'M': return pad(minutes); break; case 'p': return hours > 11 ? 'PM' : 'AM'; break; case 'S': return pad(this.getUTCSeconds()); break; case 'w': return day; break; case 'y': return pad(this.getUTCFullYear() % 100); break; case 'Y': return this.getUTCFullYear().toString(); break; } }.bind(this)); } });UPDATE: Bugs fixed. Thanks Andrew and Stephen
Also, some of my old and new code has been posted on GitHub. You might find something useful.
-
Feb04
A beautiful, organic animation by Flight404 created using Processing.
-
Jan08
Aaron Patterson shows us how to define ruby methods which can be called from JavaScript using RKelly.
permalink -
Jan07
Dustin might not surprise you at your house with a book in hand, but he’ll damn sure send you his innermost fu in the form of a shiny yellow and black book. It’s an excellent read if you’re looking to put more funk in your functions and class in your classes. Check out “Pro JavaScript Design Patterns” by Dustin and Ross Harmes.
—I’m Justin Palmer and I approve this message.
permalink -
Dec23
IE 8 PAsses the Acid 2 Test for those not keeping tabs on things.
permalink -
Oct24
Mislav has written up an excellent overview on upgrading Radiant (which used 1.5) to Prototype 1.6. The article does a great job of showing how 1.6 is superior to 1.5.
permalink -
Oct19
According to the New York Times, the Daily Show Archives, with over 7,000 videos are online—free and searchable.
permalink -
Oct11
Copyright © 2005-2009 Justin Palmer | Powered by Mephisto

Discussion
Thanks Brian Donavan!
Thanks for getting the word out. Your readership is much larger than mine (which is probably around 4, hehe).
I noticed you spelled my name wrong though. Donavan is a common misspelling.
oops! Corrected now. I get referred to as ‘Jason’ all the time if it’s any consolation. ;-)
Yeah! Woohoo!
Great site and great projects!
Great stuff Justin, would it be possible to mimic the register/apply approach of behaviour which permits adding rules at different level ? For example, i use a decorator pattern for my pages and both my decorator and pages use some rules.
I could of course wrap your stuff and work around this limitation but i think a 2 phase approach(first register some rules, can be called N times to add more rules, and then later on apply those rules) could really benefit your event:Selectors
RFE:
Thanks in advance.
Chris, this sounds like a useful and practical idea. Do you mind creating a ticket for it: http://encytemedia.com/treasure/
Are css3 selectors supported?
Eg.
[att^=val]
Represents an element with the att attribute whose value begins with the prefix “val”.
[att$=val]
Represents an element with the att attribute whose value ends with the suffix “val”.
[att*=val]
Represents an element with the att attribute whose value contains at least one instance of the substring “val”.
Any chance this update is available on a site NOT built on RubyOnRails (or one that doesn’t give a useless precondition failure when I go there)?
http://dev.rubyonrails.org/browser/trunk/railties/html/javascripts/prototype.js?format=raw
Hi,
I’ve just added a patch for attribute selectors which makes them fully IE6 compatible.
It also adds support for
*=,^=and$=operators and replaces!=(which is not part of the specs) with the spec friendly:not(X)which can be applied to any of the attribute selectors:Check the updated test file for more possible uses.
Comments are more then welcome. Please be nice though: this is my first patch proposition for Prototype.