When calling a custom plugin, how can I get the current selector string?
$('my_selector p').my_plugin();
Would like to output my_selector p
within my script. How can I access this string?
When calling a custom plugin, how can I get the current selector string?
$('my_selector p').my_plugin();
Would like to output my_selector p
within my script. How can I access this string?
Post selector deprecation v. 1.7:
If you're only dealing with ids and classes as selectors you can use the following:
There are cleaner ways but since the removal of .selector due to being inconsistent between browsers since 1.7, this has been my go-to.
Being deprecated, the official advice is to add the selector as a parameter in your function call: https://api.jquery.com/selector/
Redundant, yes, but it's always reliable.
With sector deprecated i use
just two lines of code
or if you're really cheap with lines
You can use
selector
property:version deprecated:
1.7
, removed:1.9
Extending to kevinmicke's answer : You can get the selector in your event object that you pass on callback functions.
In callback functions
Example:
You'll get the selector string in e.handleObj.selector
Console Log gives an object like this: