I have the following Javascript that runs in my popup window and which uses jQuery to select the checked checkboxes from the parent window and then for each checkbox it again uses jQuery to select associated data from hidden fields (also from the parent window).

var chked = $('[name^="mycheckbox_"]:checked', opener.frmMain);
var hdn;
var fieldId;
var data = {};
$.each(chked, function (key, field) {

    hdn = field.id.replace('chk_', 'hdn_');

    data[hdn + 'id'] = $('[name="' + hdn + 'id"]', opener.frmMain).val();
    data[hdn + 'name'] = $('[name="' + hdn + 'name"]', opener.frmMain).val();
});

The issue I'm encountering is that it can take over a minute to execute the $.each loop when there are over a thousand checkboxes. If I comment out the following lines in the $.each loop then the performance improves significantly:

data[hdn + 'id'] = $('[name="' + hdn + 'id"]', opener.frmMain).val();
data[hdn + 'name'] = $('[name="' + hdn + 'name"]', opener.frmMain).val();

I'm trying to figure out how to improve the performance and yet still load the associated hidden fields. One option could be to use jQuery to query the hidden fields up-front before the loop executes and then perform a lookup in the array returned from this query in the loop. However I'm not sure that performing an array lookup within the $.each loop would be any quicker than the existing jQuery calls. Would appreciate any input.

Related posts

Recent Viewed