Bug Tracker

Ticket #8332 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

wrapAll with <tr> does nothing

Reported by: Marc Bourlon Owned by:
Priority: low Milestone: 1.next
Component: manipulation Version: 1.5
Keywords: Cc:
Blocking: Blocked by:

Description

var tds = $('<td></td><td></td><td></td>'); tds.wrapAll('<tr></tr>');

does nothing! Maybe I'm wrong here, but I stumbled upon this because one of my jQuery template, a tr containing some tds, was transformed into only the list of tds. Trying to insert a wrapAll('<tr></tr>') before the appendTo table was still failing.

Change History

comment:1 Changed 3 years ago by jitter

  • Priority changed from undecided to low
  • Resolution set to invalid
  • Status changed from new to closed
  • Component changed from unfiled to manipulation

Thanks for taking the time to contribute to the jQuery project by writing a bug report.

This isn't a bug but just a slight misunderstanding on how .wrapAll() works and what happens with the jQuery object (the collection it represents) when calling .wrapAll().

If you call .wrapAll() on a jQuery object all elements in the collection get wrapped with the HTML structure you provided, but that doesn't mean that after the call the collection suddenly changed and now holds the "new" parents of the before wrapped elements. In fact the collection doesn't change at all and still holds the same elements as before so that you can continue chaining methods and make further e.g. modifications to the elements.

In the case of elements attached to the DOM this often goes by unnoticed (especially when you don't have any other methods chained after the wrappAll) as the new parent (parents) are directly inserted into the DOM and you immediately see it worked.

In the case of detached elements (as in your $("<td></td><td></td><td></td>")) you need to be aware of this. Because else when you call .appendTo() it appends the td's the collections holds to the target, instead of the tr as you intended. So you only need to call .parent() before the call to ´.appendTo()`.

Also check this  test case which illustrates how .wrapAll() works with attached and detached elements

Last edited 3 years ago by jitter (previous) (diff)

comment:2 Changed 3 years ago by anonymous

Thank you very much jitter, I was thinking about something like this, but didn't think about parent(). However, it doesn't explain the problem, this time, I think, a bug, seen in using the template system, dropping the outermost tr from the template. Let me try to do an example with jsFiddle, too ;-) Best,

Note: See TracTickets for help on using tickets.