Pick a plugin selector

3

I'm trying to create a plugin but I can not get selector , and it was still removed from version 1.9.

If I use $(this).selector in the plugin, it returns a string #div #elementoA, #div #elementoB . I've already tried to use this.each to try to pick individually, but I could not.

I need a form that returns either an array, or an object, but with the individual selectors: '[#div #elementoA] [#div #elementoB] . Is it possible using 1.9?

$('#div #elementoA, #div #elementoB').plugin()
    
asked by anonymous 08.12.2015 / 08:38

1 answer

2

It seems like there's no way around it.

The documentation even gives an example where a plugin that needs to know about the selector should get it repeated

strong> as a function parameter.

Sample declaration:

$.fn.foo = function( selector, options ) { /* código */ }; 

Example call:

$( "div.bar" ).foo( "div.bar", {dog: "bark"} );

It is not a satisfactory answer, but it is the official answer.

I think it would be best to rephrase your code so as not to depend on the selector if possible, otherwise it would be possible to create a more beautiful alternative than the repetition. A very simple and unsophisticated example would be:

function applyPlugin(selector, options) {
    $(selector).plugin(selector, options);
}

If possible, edit the question explaining exactly what you could not do with each ".

In any case, it is the model of the use of each :

$.fn.myNewPlugin = function() {
    return this.each(function() {
        // ação em cada elemento aqui
    });
};
    
08.12.2015 / 09:01