Creating Your Own jQuery Custom Selector

Posted by: Suprotim Agarwal , on 8/16/2011, in Category jQuery and ASP.NET
Views: 134121
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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.