ARTICLE

JQuery and CSS Selectors: Part 3

Posted by Abhimanyu K Vatsa Articles | JQuery February 11, 2012
In this article you are going to learn about jQuery and CSS Selectors. If you are still not using jQuery in your web applications, then this article will motivate you.
Reader Level:

Introduction

In this article you are going to learn about jQuery and CSS Selectors. If you are still not using jQuery in your web applications, then this article will motivate you.

Here I am going to talk about "Attribute Selectors", let's first look at a problem that we will solve in this article.

Problem

Suppose you have a web page (or website) and you want to apply the style to all links on the web page (or website) in such a way that the display styles are as in the below image for each type of links email URL, PDF file download URL and Word file download URL, differently:

1.gif

I have image, you can see two different pages, an HTML page and an ASP.NET page. I am an ASP.NET guy so I love to use this too.

Now, to create such system we need to identify the URL Attributes (which containing email or pdf or word) and add the image with url text. So, we need to take the advantage of "Attribute Selectors" here. Two things very important to learn here "Attribute Selectors" and "Wildcard Characters".

Attribute Selectors

Attribute Selectors allow us to specify an element by one of its HTML attributes such as a link's "title" attribute or image's "alt" attribute etc.

For example, if you want to select all images that have an "alt" attribute:

$('img[alt]')

Another example, if you want to select all links that have a .pdf extension at the end:

$('a[href$=".pdf"]')

In above example, I'm using a sing "$" with href that's Wildcard Character. Let's learn it.

Wildcard Characters

Attribute Selectors accept a wildcard syntax 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.

Examples:

  1. To select all links that is 'a' and its attribute href, ends with ".pdf" exetension:

    $('a[href$=".pdf"]')
     
  2. To select all email links that is also 'a' and its attribute href, starts with "mailto" expression:

    $('a[href^="mailto:"]')

Now, it's time to look at the solution. Let's create some styles that we will add to link's (that is "a") attribute "href". Look at all my styles:

  <style type="text/css">
        a
        {
            
color:Green;
        }
        a.email
        {
            
background: url(images/emailicon.ico) no-repeat right top;
            
padding-right: 20px;
        }
        a.pdf
        {
            
background: url(images/pdficon.ico) no-repeat right top;
            
padding-right: 20px;
        }
        
a.word
        {
            
background: url(images/wordicon.ico) no-repeat right top;
            
padding-right: 20px;
        }
    
</style>


In the above style, I have created four different styles. The first one is for all link's (that is 'a') commonly, the second one is for mailto links and the other two are for .pdf and .doc extensions.

Now let's move on to jQuery methods that will add the above styles to appropriate links.

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    
<script type="text/javascript">
        $(document).ready(
function () {
            $('a[href^="mailto:"]').addClass('email');
            $(
'a[href$=".pdf"]').addClass('pdf');
            $(
'a[href$=".doc"]').addClass('word');
        });
    
</script>

Note: Don't forget to add the reference of your jQuery library, as I have added that in the above code.

Now, if you want to incorporate such a system in your entire website then you just need to add the above code in the Master Page (in case you are an ASP.NET guy) or just create the independent js file and place its reference on web pages.

Download the attached project and run it yourself.

So, that's all about "Attribute Selectors". In the next part you will learn "Custom Selectors", this is also going to be a enjoyable title. Thanks for reading.

Login to add your contents and source code to this article
post comment
     

Really this is very good article, thanks Abhimanyu

Posted by Nitin Singh Feb 13, 2012

I will surely try this.

Posted by Laura Parker Feb 13, 2012

Hi Abhimanyu, It's really help for beginners. Thanks for sharing.

Posted by Manish Singh Feb 13, 2012

Hi Abhimanyu. You have presented your article very nicely. Thanks for sharing.

Posted by Stephen Johnson Feb 13, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts