Bug Tracker

Ticket #11438 (closed bug: invalid)

Opened 3 years ago

Last modified 2 years ago

Non-persistence of events when reappending dom nodes to their original parent

Reported by: wheresrhys Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocking: Blocked by:

Description

I'm not sure if this is a bug or intended behaviour, but when using .html() to reappend elements elements to their original parent then any jQuery events for these elements are lost. This desn't happen when appending the elements to a different node using html() or when using append() to append the elements to the existing parent.

 http://jsfiddle.net/wheresrhys/tPcEE/

Obviously, appending an element to its existing parent using html() is not something you'd normally do on purpose, but could crop up when iterating over a large number of nodes which may/may not have been moved by other processes in your app.

As html() is only documented as accepting strings I reckon this is probably a won't fix, but thought I'd bring it up just in case.

Change History

comment:1 Changed 3 years ago by dmethvin

  • Keywords needsdocs added
  • Status changed from new to closed
  • Resolution set to invalid

Internally, .html(x) calls .empty().append(x) for x that isn't a string. So knowing that it should be clear that .empty() is removing the events. But I agree that it is not a good idea to pass anything but a string to .html() since that is what it is documented to accept. At most we might want to add a note to this effect, but I am not even sure about that. ("Warning: only pass a string to .html() yeah I'm talking to you, like it tells you to do.")

comment:2 Changed 3 years ago by wheresrhys

I actually think it's a problem in general with jQuery/jQuery docs that jQuery works for a lot more cases than it officially supports. As you said above, there is in fact some specific code within .html(x) for when x is not a string - if jQuery truly didn't support this then it should just throw an error/do nothing here. I'm not arguing for this feature to be removed/deprecated as it's generally useful, but this kind of grey area exists all over jQuery - there's been a few times when I've reported/thought about reporting a "bug" only to find that it's an instance of jQuery not supporting what it, surprisingly, doesn't officially support. So it's not too outlandish a suggestion to document in greater detail the limitations of jQuery's functionality when it so often steps beyond what it says it supports.

comment:3 Changed 3 years ago by dmethvin

People misuse the undocumented edges of every interface we offer. We're not going to add more code to break the edges, that is pretty user-hostile.

comment:4 Changed 2 years ago by dmethvin

  • Keywords needsdocs removed
Note: See TracTickets for help on using tickets.