how to override the existing function in jquery [duplicate]
Asked Answered
W

3

5

This is default for all js

getEditor: function(){
    $( '#datatableEditor' ).remove();
    var editor = $( '<div id="datatableEditor" class="popupEditor"/>' );
    $( 'body' ).prepend( editor );

    var dialog = $(editor).dialog({
        title: 'Edit item',
        modal: true,
        width: 'auto',
        height: 'auto'
    });

Now I am writing another js. I need to override the getEditor in my js...

Waterlog answered 17/2, 2014 at 7:43 Comment(2)
what you want to change / implementBeggs
i need to change the title. it is same for all but i need to change title as action nameWaterlog
C
10

You haven't described your question, clearly but based on what you have mentioned in the title:

override the existing function in jquery

It seems you want to change a jQuery function and they usually are defined like $.fn.getEditor if it is the case, you should do:

(function ($) {
    var oldGetEditor = $.fn.getEditor;
    $.fn.getEditor = function() {

       //you can call the oldGetEditor here if you want
    };
})(jQuery);
Catawba answered 17/2, 2014 at 8:2 Comment(0)
C
0

You simply get a reference to the function "getEditor" and assign it another function.

getEditor:function(){
// new function will override the existing reference that getEditor already has.
}
Clotho answered 17/2, 2014 at 7:46 Comment(0)
D
0
someObject = {
 getEditor: function(){
  $( '#datatableEditor' ).remove();
  var editor = $( '<div id="datatableEditor" class="popupEditor"/>' );
  $( 'body' ).prepend( editor );

  var dialog = $(editor).dialog({
    title: 'Edit item',
    modal: true,
    width: 'auto',
    height: 'auto'
  });
}

So just write

someObject.getEditor = function(){
  // your own logic here
}

You must get object where getEditor function stored, and use reference to change it.

Dobruja answered 17/2, 2014 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.