Can I get a reference to an object created in a jQuery plugin?

Posted by gargantaun on Stack Overflow See other posts from Stack Overflow or by gargantaun
Published on 2010-05-06T19:36:04Z Indexed on 2010/05/06 19:38 UTC
Read the original article Hit count: 211

Filed under:
|

Perhaps my brain is fried, but I'm writing a plugin that created an tweaks an element, but also creates an object that i'd like access to. So the plugin looks like this

(function ($) {
  $.fn.myPlugin = function () {
    return this.each(function () {

          // do some stuff to the element...

          this.objectInstance = new usefulObject();

    });
  };
})(jQuery);

function usefulObject(){
    // useful object properties and methods....

    this.doSomething = function(){
        alert("Don't google Google. You'll break the internet.");
    }
}

so when I call the plugin, I also want to be able to get access to that usefulObject that I created. I thought something like this might work....

tweakedElement = $("#someDiv").myPlugin();

tweakedElement.objectInstance.doSomething();

... but that's not working. How can I achieve this? Can I achieve this? Answers on a postcard, or down below, whichever suits you.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript