wrap() working only on elements that are part of document #638

wants to merge 1 commit into


None yet
2 participants

dandv commented Nov 11, 2012

This is the no. 1 comment on jQuery's documentation of wrap - that you have to inject the element in the DOM first.

However, it's not at all clearly intuitive to a new user why $('<p class="new-element">').wrap("div") wouldn't do anything.


mislav commented Nov 11, 2012

I see what pitfall you are trying to address and I support you.

But, you can wrap a node that's not yet part of the DOM.

el = $('<em>foo</em>').wrap('<li>')
el.parent() //=> LI

Now EM is wrapped in LI, but they are both still detached from the DOM, and the el variable is still a reference to EM not LI. After you inject EM into the document, you've moved it from its parent LI element and into another element, therefore the effect of wrap() is lost.

mislav closed this in 8b91daf Nov 25, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment