Creating Your Own jQuery Custom Selector
Posted by: Suprotim Agarwal ,
on 8/16/2011,
in
Category jQuery and ASP.NET
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:
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.
That’s it. Our selector :mailToLink is ready and here’s how we can use it:
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.
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.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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