wrap()
creates a HTML structure on the fly and inserts it into the DOM, that's why something like :
$(element).wrap('<div><span></span></div>')
works, so the variable $wrapper is not the same as the element that was actually created and inserted by wrap()
, so you have get the element from the DOM again :
var $holder = $("#d");
var $wrapper = $('<div />', {style:'border:1px solid red;', id:'wrapper'});
$holder.wrap($wrapper);
$holder.detach();
$('#wrapper').append($holder);
from the documentation:
The .wrap() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.