Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the addBack function to jQuery version 1.7.2

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.

like image 884
Daft Avatar asked Jul 23 '13 11:07

Daft


People also ask

What is addBack in jQuery?

add() Adds elements to the set of matched elements. addBack() Adds the previous set of elements to the current set. andSelf()


1 Answers

To extend jquery methods:

$.fn.addBack = function (selector) {
    return this.add(selector == null ? this.prevObject : this.prevObject.filter(selector));
}
like image 63
A. Wolff Avatar answered Oct 08 '22 13:10

A. Wolff