Does this structure cause memory leaks and how to resolve (strategy suggestion)?. We have a page containing many dynamically generated elements (tables, buttons, textfield, checbboxes, comboboxes etc) depending on business rules and user actions. We do not have any static page. Every element is generated from a java servlet with ajax-responses using json.
We have a main container div which contains all controls. Sometimes we nee need to remove all contained elements and put new ones and sometimes only update some properties such as value for example.
<div id="bigContainer"></div>
When generating an element, we generate its html and store it in the bigContainer using jquery. For example, suppose we have a button and a textfield on first step.
var jsButton = '<div id="button1wrapper"><input id="button1" type="button" onclick="buttonAction(this)" style=".."></input></div>';
var jsTextfield = '<div id="text1wrapper"><input id="text1" type="text" onfocus="focusAction(this)" style=".."></input></div>';
And our current page needs only this two elements:
var currentPage = jsButton + jsTextField;
And use jquery to set the elements.
$('#bigContainer').html(currentPage);
After some user interactions, we do not need these elements anymore and a generate for example a checkbox.
var jsCheckbox = '<div id="check1wrapper"><input id="check1" type="checkbox" onclick="checkAction(this)" style=".."></input></div>';
And I only need this checbox currently, so i set it to the page:
$('#bigContainer').html(jsCheckbox);
As you see, I did not make any cleanup (i did not removed any element). Is there any bad-practise? Memory leaks? Event handlers? Zombie dom-objects?
And what happens on the second scenario, I generate a table but register its listener with jquery bind method:
var jsTable = '<div id="table1wrapper"><table id=table1><tr><td>colval1</td><td>colval2..</table>';
jsTable += '<script>registerTableCellListeners("table1")</script>';
$('#bigContainer').html(jsTable);
As you see, the table js contains a function call, registerTableCellListeners which is invoked, once the table has been generated and put into the DOM.
When the table has been created and this function is called, it registers cell events:
function registerTableCellListeners(id) {
$('#id').find('td').bind('click', function() {
// handle cell event
And for example after some useraction we do not need the table anymore, generate a button and set to container and it continues like this.
$('#bigContainer').html(jsButtonNew);
Question is, do we need to call jquery method remove()
on elements when we do not need them anymore, or remove event listeners especially for the table? Is any possiblity of memory leak of this mechanism? And about performance?