Robert Hane
Robert Hane

Reputation: 37

extending function in jquery

I have tried to extend my jquery the following way, it isn't working.

var y = {
    alertData: function () {
        alert('z');
    },
    hideData: function () {
        $(this).hide();
    }
};
var z = $('#a');
$.Extend(z, y);
z.hideData();

Upvotes: 0

Views: 46

Answers (1)

krishwader
krishwader

Reputation: 11371

Maybe try extending like this?

$.fn.extend({
    alertData: function () {
        alert('z');
        return this;
    },
    hideData: function () {
        this.hide();
        return this;
    }

});

And using it like this on an element with a as id?

$('#a').alertData().hideData(); 

The reason your implementation wasn't working was :

  • The typo, its not Extend, its extend
  • Its this, not $(this) inside extend

Demo : http://jsfiddle.net/hungerpain/TkA2e/

Upvotes: 1

Related Questions