Now that you have provided us with more of the body of the buildFieldItem() function, I see another possible leak.
Without the full code in runnable state, I cannot say for sure, but when you set $field.attr('data-ord', fieldDataObj.ord) , if .ord is any DOM link, you can store some kind of DOM link as an attribute on the $ field object. When you later do .empty() for the parent, this link may not be cleared in older versions of IE and thus it will become stuck and will not be able to receive garbage collection.
This is one of the reasons jQuery came up with the .data() method, because it does not store data on the DOM object, and it cleans up the .empty() method after itself, even in older versions of IE. Since I do not have a working version of the code, I can’t say for sure that this is a problem, but I would suggest changing:
.attr('data-ord', fieldDataObj.ord);
to
.data('ord', fieldDataObj.ord);
And then change any other links that read this attribute to use .data() to read it.
I also think that you are asking for problems with this line, since you rely on both .clone() and .empty() to be perfect in cleaning everything (even those things that jQuery did not create) and never do not lose anything in any browser:
var replacement = container.clone().empty();
It is much better to create a completely new container, since you want it to be empty, to start with any methods, as this can never leak the previous data, since it does not have:
var replacement = $("<div>");
The answer to the third disclosure of the actual contents of drawFieldElements() :
Now your code really doesn't make sense to me. You have the following:
function drawFieldElements(fields, container) { var replacement = container.clone() .empty(); $.each(fields, function(i) { var field; field = buildFieldItem(i, this); replacement.append(field); }); container.replaceWith(replacement); }
You take your container. You clone and empty it. So, now you only have the top level with the class on it remaining in the clone. You add a bunch of content to the clone. Then you replace the original container with the new cloned container. You just ask for memory leak problems with all these extra DOM manipulations.
Why not just clean your original container and add it? Then there is no cloning, no replacement.
function drawFieldElements(fields, container) { container.empty(); $.each(fields, function(i) { container.append(buildFieldItem(i, this)); }); }
I have no idea if this helps in a memory leak, but clearing the code to remove all unnecessary DOM manipulations is always a step in a good direction.