The confusing part is that .wrap()
returns the inner element, not the parent element.
So you have to use the parent object of the wrapped one as follows:
var $divA= $("").addClass('classA'),
$divB= $("").addClass('classB');
console.log( $divA.wrap($divB).parent() );
($divA.parent()
is equal to $divB
after the wrapping)
So the key part is that $divA.wrap($divB)
returns $divA
, NOT $divB
see the reference:
This method returns the original set of elements for chaining purposes.
Please note: The elements DON'T have to be in the DOM, jQuery can operate on them without them already having been inserted into the DOM.