Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .selector property removed, workaround?

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?

like image 362
Scott Avatar asked May 30 '13 12:05

Scott


1 Answers

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

like image 183
cfs Avatar answered Oct 14 '22 07:10

cfs