I'm trying to get the selector used to call the current script, but of course the property I need was removed for some reason.
Is there a workaround for this? Here's basically what I want to accomplish:
(function($) {
$.fn.myplugin = function(options) {
return this.each(function() {
console.log($(this).selector);
});
}
}(jQuery));
//Somwehere else:
$('.theClassISelected').myplugin(); //Should console.log('.theClassISelected')
I need to see .theClassISelected
(or some form of the original selector I used to call the function) in the console, but since the selector property has been removed from jQuery, it doesn't possible anymore.
I don't understand why it was removed - I've Googled this problem for a while now and all I see are StackOverflow answers from 2011-2012 recommending the selector
property. I guess it was useful at some point, but not anymore?
From the jQuery documentation:
Plugins that need to use a selector should have the caller pass in the selector as part of the plugin's arguments during initialization.
http://api.jquery.com/selector/
As an aside, the docs also mention that the selector property wasn't reliable since "since subsequent traversal methods may have changed the set."
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