TruMan1
TruMan1

Reputation: 36078

How do I extend a jQuery type with new function?

I have a special type object which is really QTip2. I want to add an extra function to my qtip type so I can do something like this with my instance:

myQtip1.doSomething();

How do I extend functions on my jQuery objects?

Upvotes: 0

Views: 120

Answers (2)

Inkbug
Inkbug

Reputation: 1692

Try adding stuff to jQuery.fn -

jQuery.fn.myMethod = function () {
  //...
};

To differentiate it from other libraries' myMethod, you can call it customPrefix_myMethod (replacing customPrefix with a custom prefix of you own).

Upvotes: 1

Klaas Leussink
Klaas Leussink

Reputation: 2727

Try this:

jQuery.fn.extend({
    my_function: function () {
        // do stuff
    }
});

Then you can use it like

$('#element').my_function();

You can also use it in a chain:

$('#element').parent().my_function().show();

Upvotes: 1

Related Questions