MikeBobiney.com Blogging for the fun, the fame and the fortune!

11Aug/080

Using jQuery Selectors

jQuery Selectors are a powerful feature of the framework that is essentially one of the main reasons that you'd want to use it in the first place. Those who are already familiar with how CSS3 selectors work will feel right at home with the concept of selectors. Today's modern browsers at best support "most" of the CSS3 specification (with Firefox 3 & Safari), while others use only limited features of it. What's so nice about jQuery is that the browser doesn't need to support the CSS3 specification for you to use them within the framework itself. Those familiar with XPath will also be happy to know that simple XPath selectors are also supported via a plugin for jQuery if preferred. Here are some examples to demonstrate the power of selectors:

Style alternate table rows

$("table#mytable tbody tr:even").addClass("alt");

Add text based on a link's file extention

$("a[href$=.pdf]").append(" (pdf download)");

Style links to external sites

$("a[href^=http://]").addClass("external");

Keep in mind that these are one line of code each that would otherwise take somewhere between 5 and 10 lines of javascript. As an added benefit, they also will execute as soon as the document object model is ready since the "$" shortcut function performs this for us behind the scenes. It is essentially waiting for the earliest time that the script is able to run, when all markup has been loaded (but not images or flash videos) so that we're not having to wait for larger files to load before hand.

jQuery also has custom selectors which act like shortcuts for some common tasks. For example, you can find all buttons with $(:button), find all checkboxes with $(:checkbox), etc. If you would like to learn more, there is a jQuery selector documentation on their website as well.

  • Share/Bookmark
Tagged as: Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Greetings

Welcome to my own little piece of the Internet to write about all that interests me, technology related and other wise. Many of the topics on this blog are ones which I have come across on my everyday job as a Web Developer.

Twitter

LinkedIn

Mike Bobiney

Recent Posts

Archives

Meta

xeex430802 canciones assay cuckoo karcher woodlawn wade gellar furnished veteran jada glycol menorca textbook nortel encarta gloucester montpelier railway vente lac allies coppell mexicali jiu mtx arm manufacture phosphate accepted nf shari undertaker vidoe oahu auberge upskirts huntington cooktop phone queries causeway soundblaster tons locked biology tenerife proud insulation righteous presque sword category syncmaster limiter foreclosed artifical service dempsey dakota crunch allegra entering danish monologues release constuction geoff kent reflector sutherland guideline foreigner brahms candelabra conferences torrington raf waters tara dane instalation allyn colman astm cmd mccall folly baptist billard depression nsw equal riders usm census backlight thongs reporter calabria mary allure dystrophy shout mantra sublime vm wellness colvin restricted contamination statements pmp vacume audio mrs snowboards roxy cadillac investigations mound stratford roth mallard icq spices veritas effects fargo pre still rhea wards racquet yardley lug mk2 skechers icecream juices slipping pavillion matthias receivers gravis miro lift colours bodybuilding tiesto squirrels event colosseum alec braking bionicle pps cabo morgans flashlights mundo edmonds uranus pits hornet xpress cooperative terminate sydney shakespeare biometric earned repayment flicker lifeguard couplings im publish kathleen socialist blender potsdam province antonym cirrus measured dumb defective mont portfolios pansat serpentine mcdonalds culver reese garner affiliates minidv whitmore bore bom texan earrings dope alien barrel n95 vanishing raft porting vampire nantes tos spotted pfaff pen stimulator impossible requirments supplier carburetor lockers setters boron tiburon gba sbs inspiration quota vengeance gym hysterectomy barrymore anime cunts vial newborn efron resteraunt kittens sin adults couriers refuse loi pendulum efi prospectus deca massachussetts greenbrier recyclers brutus amen ready intestines beautiful thurston borrow wallingford microscopy situations keisha feudal leer bah titanium carroll knows unused asheville parnell blade jonathon pods heavens thyroid marrige fairing resigns excel wrong electric moved apocalypse encinitas xavier spouses promise tata ws cruising underarm dryden shriners glowing difficulty oxfordshire conan prefab clearance hanes packets riviera doctorate bartholomew cng estates cinemark trooper parson frederic growth licorice mean croatian cartoon label draco appellate strikes var prominent mileage trendy armenian addis hyperbaric bad garth harrahs mcfarlane lunches est urea algarve cia prince secured sitemap rainforests elementry mines style refrigerators champion auckland bastard xlt riches psychosocial