DotNetCurry Logo

Creating Your Own jQuery Custom Selector

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

Please share this article if you think it was worth reading. Thanks!
Share on Google+
Further Reading - Articles You May Like!
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




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