Get the current jQuery selector string?
You can use selector
property:
$('my_selector p').selector // my_selector p
version deprecated: 1.7
, removed: 1.9
Post selector deprecation v. 1.7:
If you're only dealing with ids and classes as selectors you can use the following:
var selector = (typeof($(this).attr('id')) !== 'undefined' || $(this).attr('id') !== null) ? '#' + $(this).attr('id') : '.' + $(this).attr('class');
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/
Plugins that need to use a selector string within their plugin can require it as a parameter of the method. For example, a "foo" plugin could be written as
$.fn.foo = function( selector, options ) { /* plugin code goes here */ };
and the person using the plugin would write
$( "div.bar" ).foo( "div.bar", {dog: "bark"} );
Redundant, yes, but it's always reliable.