If an element doesn’t exist, how do I get a string of the selector that was searched?
For example:
$( 'parent child.class' ).plugin( )
In my plugin, I want to get the string "parent child.class", but $( 'parent child.class' ) doesn’t exist.
I’m going to start digging through the jQuery source code, but I figured I’d ask in case someone with more intimate knowledge knows this.
You can’t.
jQuery objects have a
.selectorproperty, which you could use within your plugin (though it seems to be intended for use interally with jQuery), but – and it’s a big but – not all jQuery objects are created with a selector string. Consider this example use of.yourPlugin():It doesn’t make sense to be thinking about what selector resulted in the jQuery object eventually passed to
.yourPlugin(), regardless of whether it contains any elements.(I suppose if you know your plugin will only ever be used by you and you’ll only ever pass it jQuery objects created with a selector then you could use
this.selectorwithin your plugin…)