I am making a jQuery plugin, and I was wondering if there was a way that I could find the selector that the user uses to apply the plugin. For example, if the user selects this:
$(".myClass").pluginName();
Then the plugin will return myClass
, which can then be used later. Is there a way I can do this?
jQuery - find( selector ) Method. Description. The find( selector ) method searches for descendant elements that match the specified selector.
.find (selector) Returns: jQuery Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. version added: 1.0.find (selector)
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
A jQuery object like $ (“li”). Note – Use comma (,) in order to separate each value that you want to find. Use * to find all the descendants of the selector. Let us understand the jQuery .find () Method by seeing few examples. I give you 6 examples of .find () method. 1. Using jQuery .find () Method to Find Elements
Inspecting the jQuery object in the debugger, it has an attribute selector
that contains the selector used to create the set. I don't know if this is public API that you can count on remaining available in future releases or if this is an implementation detail that may change without warning.
Like the other commenters, I question the need to reference the selector. In your plugin you can simply iterate through all of the selected elements without performing the search through the DOM again. Just use this.each()
in your plugin.
Suppose this is your selection :
var $selection = $('a, i');
If you want to get the selector, just use the selector
property :
var selector = $selection.selector;
In this case, the value of the selector
variable would be the string a, i
.
While the selector
property is included in jQuery 1.11.0 and jQuery 2.1.3, it appears to be missing from jQuery 3.0.0 alpha1. This is probably why :
The
.selector
property was deprecated in jQuery 1.7 and is only maintained to the extent needed for supporting.live()
in the jQuery Migrate plugin. It may be removed without notice in a future version.source
If you can think of a good reason why this feature should stay in the jQuery core, you might want to make an appeal to the jQuery dev team and request they change their decision to remove the feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With