Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find selector used jQuery

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?

like image 448
yainspan Avatar asked Sep 11 '15 14:09

yainspan


People also ask

What is the use of find selector method in Ajax?

jQuery - find( selector ) Method. Description. The find( selector ) method searches for descendant elements that match the specified selector.

What does find (selector) return in jQuery?

.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)

What are jQuery selectors?

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.

How to find all descendants of a jQuery selector?

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


2 Answers

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.

like image 38
dsh Avatar answered Sep 20 '22 21:09

dsh


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.


NOTE :

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.

like image 145
John Slegers Avatar answered Sep 22 '22 21:09

John Slegers