views:

215

answers:

2

Hi,

I am migrating my js lib from prototype to jquery. However, I don't know how to replace the following code:

var utilityMethods = {     
        autoHide : function(element) {
               //...
}

Element.addMethods('SPAN', utilityMethods);

Is there a jQuery equivalent for extending the DOM?

Thanks

+1  A: 

Actually, jQuery specifically avoids extending the DOM. Having recently completed a migration from Prototype to jQuery, this was one of the selling points for me. Instead, you extend jQuery itself. Selecting a DOM object with jQuery gives you a jQuery object that contains a reference to one or more DOM objects. Any method calls on the jQuery object (including your custom utility methods) operate on the DOM elements referenced by that jQuery object.

Joel Mueller
+2  A: 

You extend JQuery objects like so:

var utilityMethods = {     
    autoHide : function(element) {
           //...
    }
};

jQuery.fn.extend(utilityMethods);

More info: http://docs.jquery.com/Core/jQuery.extend

Simon David Pratt
A minor correction:jQuery.fn.extend(utilityMethods);
Miguel Ping