MOBILE

jQuery 1.3 : Selectors - Attribute selectors

8/24/2012 3:27:11 AM

Att&;ribute selectors are a particularly helpful subset of CSS selectors. They allow us to specify an element by one of its HTML properties, such as a link's title attribute or an image's alt attribute. For example, to select all images that have an alt attribute, we write the following:

$('img[alt]')

In versions prior to 1.2, jQuery used XML Path Language (XPath) syntax for its attribute selectors and included a handful of other XPath selectors. While these basic XPath selectors have since been removed from the core jQuery library, they are still available as a plugin: http://plugins.jquery.com/project/xpath/


Styling links

Attribute selectors accept a wildcard syntax inspired by regular expressions for identifying the value at the beginning (^) or ending ($) of a string. They can also take an asterisk (*) to indicate the value at an arbitrary position within a string or an exclamation mark to indicate a negated value.

Le&;&;t's say we want to have different styles for different types of links. We first define the styles in our stylesheet:

a {
attribute selectorsstyles, definingcolor: #00c;
}
a.mailto {
background: url(images/mail.png) no-repeat right top;
padding-right: 18px;
}
a.pdflink {
background: url(images/pdf.png) no-repeat right top;
padding-right: 18px;
}
a.henrylink {
background-color: #fff;
padding: 2px;
border: 1px solid #000;
}

Then, we add the three classes — mailto, pdflink, and henrylink — to the appropriate links using jQuery.

To add a class for all email links, we construct a selector that looks for all anchor elements (a) with an href attribute ([href) that begins with mailto: (^=mailto:]), as follows:

$(&;document).ready(function() {
$('a[href^=mailto:]').addClass('mailto');
});

To add a class for all links to PDF files, we use the dollar sign rather than the caret symbol. This is because we're selecting links with an href attribute that ends with .pdf:

$(document).ready(function() {
$('a[href^=mailto:]').addClass('mailto');
$('a[href$=.pdf]').addClass('pdflink');
});

Attribute selectors can be combined as well. We can, for example, add a henrylink class for all links with an href value that both starts with http and contains henry anywhere:

$(document).ready(function() {
$('a[href^=mailto:]').addClass('mailto');
$('a[href$=.pdf]').addClass('pdflink');
$('a[href^=http][href*=henry]')
.addClass('henrylink');
});

With the three classes applied to the three types of links, we should see the following:

Note the PDF icon to the right of the Hamlet link, the envelope icon next to the email link, and the white background and black border around the Henry V link.

Other  
 
Most View
Looking Good, Sounding Great (Part 1) : Q Acoustics Q7000i 5.1, Bowers & Wilkins MT-50, Canton Movie 1050
Booting on HP 9000 Servers (part 2) - The setboot Command, Boot Console Handler (BCH) and Processor Dependent Code (PDC)
Sony Xperia ZL Review - A Powerhouse Phone In An Amazingly Compact Chassis (Part 3)
Long Awaited Canon Super Telephoto Emerges From The Wild
Sling Media Slingbox Pro-HD
Ouya Gaming Machine Review - Founding Backer Version (Part 1)
Seasonic And Enhance Junior PSUs - Compact Power Supply (Part 5) : Enhance ENP-7025E
SQL Server 2008 : Common performance problems (part 1) - Procedure cache bloating
ECS Z77H2-A2X v1.0 - Golden LGA 1155 Mainboard From The Black Series (Part 6)
Booting on HP 9000 Servers (part 4) - HPUX Secondary System Loader (hpux)
Top 10
SQL Server 2012 : Consolidating Data Capture with SQLdiag - Getting Friendly with SQLdiag (part 2) - Using SQLdiag as a Service
SQL Server 2012 : Consolidating Data Capture with SQLdiag - Getting Friendly with SQLdiag (part 1) - Using SQLdiag as a Command-line Application
SQL Server 2012 : Consolidating Data Capture with SQLdiag - The Data Collection Dilemma, An Approach to Data Collection
SQL Server 2012 : Troubleshooting Methodology and Practices - Data Analysis, Validating and Implementing Resolution
SQL Server 2012 : Troubleshooting Methodology and Practices - Data Collection
SQL Server 2012 : Troubleshooting Methodology and Practices - Defining the Problem
SQL Server 2012 : Troubleshooting Methodology and Practices - Approaching Problems
Windows 8 : Accessing System Image Backup and Recovery Functionality with Windows Backup, Cloud Backup
Windows 8 : Using the Windows 8 Recovery Tools (part 2) - Push Button Reset
Windows 8 : Using the Windows 8 Recovery Tools (part 1) - Creating a System Recovery Disc, Booting to the Windows Recovery Environment