Creating Your Own jQuery Custom Selector

Posted by: Suprotim Agarwal , on 8/16/2011, in Category jQuery and ASP.NET
Views: 114544
Abstract: jQuery supports a large subset of selectors defined by the CSS3 Selectors draft standard. Additionally it also contains some very useful pseudo classes (similar to :first-child, :hover etc). Due to its extensible framework, the best part is that jQuery lets you create and define your own custom selectors with ease. In this article, we will learn how to create our own custom selector that identifies all the mailto: links on a page

jQuery supports a large subset of selectors defined by the CSS3 Selectors draft standard. Additionally it also contains some very useful pseudo classes (similar to :first-child, :hover etc). Due to its extensible framework, the best part is that jQuery lets you create and define your own custom selectors with ease.  A full list of jQuery selectors is available on the jQuery site.

In this article, we will learn how to create our own custom selector that identifies all the mailto: links on a page. Let us see some code first:

jquery-custom-selector

As you can observe, we have extended jQuery’s selector expressions under the jQuery.expr[':'] object. To learn more about this, visit the jQuery hot Selector engine called sizzle. We  have defined a custom selector called :mailToLink to which we are passing an object, which is a collection of links. This selector looks for all anchor elements that contains an href attribute matching mailto.

jQuery Custom Selector

That’s it. Our selector :mailToLink is ready and here’s how we can use it:

jQuery Custom Selector

To test this selector, we have used some hyperlinks, out of which one of them is a mailto: link. When you click the button, we use our custom selector to indentify the :mailto link and set it’s color to red. 

image

As you can see, we have extended jQuery’s expression engine with ease!

The entire source code of this article can be downloaded over here.  I hope you liked the article and I thank you for viewing it.

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by rajini on Tuesday, January 31, 2012 9:06 AM
Good Articles before i use to follow a good site www.cinterviews.com but now i feel dotnetcurry.com is good keep it up nice site
Comment posted by Muhammed Irshad on Wednesday, August 8, 2012 2:09 AM
Dear Mr. Suprotim Agarwal,

thank you for giving all  the jquery tips for visual studio. u r a legend, please keep up posting new ideas.greetings from Malaysia.
Comment posted by Ayushi Songara on Monday, September 24, 2012 2:40 AM
It is such a very helpful article. i have learned about jquery concept with the help of this.
Comment posted by pritesh on Tuesday, March 5, 2013 4:04 AM
It is such a very helpful article.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel