7

I am currently using jQuery 1.7.2. I have added a jQuery.UI dropdown element to my application but I am getting an error when I try to close it.

Object has no method addBack

I found the answer here: jsTree Object has no method addBack

Turns out the addBack method wasnt added until v1.8. I'm not able to update to 1.8 for a few reasons, so is it possible to add the addBack function to v1.7.2?

I think this is the correct addBack function:

 addBack: function( selector ) {
                return this.add( selector == null ?
                 this.prevObject : this.prevObject.filter(selector)
           );
   }

Any advice or assistance would be greatly appreciated.

3
  • Why not upgrading your jquery version? Or why not just use andSelf() method?
    – A. Wolff
    Jul 23, 2013 at 11:49
  • Our entire system has to be updated and that will take a very long time. Whats this andSelf() method?
    – Daft
    Jul 23, 2013 at 11:51
  • andSelf() is the same method as addBack(), just it is now deprecated
    – A. Wolff
    Jul 23, 2013 at 11:53

1 Answer 1

21

To extend jquery methods:

$.fn.addBack = function (selector) {
    return this.add(selector == null ? this.prevObject : this.prevObject.filter(selector));
}
4
  • Thanks! Should I place this anywhere in particular? The bottom of the library maybe?
    – Daft
    Jul 23, 2013 at 11:54
  • As soon as jquery library is loaded and before using this method
    – A. Wolff
    Jul 23, 2013 at 11:55
  • I think the "Or better" is worse, since andSelf doesn't filter by selector.
    – sferrari
    Nov 5, 2021 at 8:47
  • @sferrari you are correct. I edited post. Thx
    – A. Wolff
    Nov 5, 2021 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.