0

I've a basic jquery plugin where uses data attributes data-something="thing" to tell the plugin what function run inside of it.

I was using the next to do it:

// Plugin definition.
$.fn.plugin = function( options ) {
    // Iterate and reformat each matched element.
    return this.each(function() {
        var func = element.data('something');
        var response = plugin[func]($(this));
    });
};

And it works just fine. But i wanted to follow the jquery standars where functions should be called:

$.fn.plugin.thing($(this));

So...the question. How to archive that? Is that possible when the same of the function comes in text?

Thanks!

2

1 Answer 1

1

If I am interpreting your question correctly, you're looking to be able to use $.fn.plugin.func instead of $.fn.plugin[func], where func is a variable storing a string that is the method name? If that is the case, I don't think it is possible because JavaScript will be looking for a method named func in the plugin variable as opposed to the method named after the string contained in the func variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thats what I tough. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.