Forum Moderators: open

Message Too Old, No Replies

3131 Form Elements & ShowPreview()

         

scoobydoo987

7:04 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



The problem is that there are 3,131 form elements and this code has to loop thru them all which makes the page lock up and freeze. What it is doing is making a div PopUp on the page. Can anyone think of a better and faster way to accomplish this?

function showPreview(vid) {

var oForm = document.forms[0];
var iNumElems = oForm.elements.length;
var oElem;

eval("divPreview" + vid + ".style.visibility = 'visible'");

for (var i = 0; i < iNumElems; i++) {
oElem = oForm.elements[i];
if (("SELECT" == oElem.tagName) && =
(oElem.name!= "descript_" + vid) &&
(oElem.name!= "DescriptionAdj_" + vid)){
oElem.style.visibility = 'hidden';

}
}
}

Bernard Marx

9:30 am on Jan 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




1)
oElem
should be a local variable (a little faster)

2) You could use a reg exp to do both those name tests at once, avoiding the concatenations too.

Compile the reg exp at the top of the loop:

var regNames = new RegExp("^(descript_"+vid+"¦DescriptionAdj_"+vid+")$");

Then the test is:

[red]![/red]regNames.test(oElem.name)

But, I suspect that the whole process has started on the wrong foot.
So I recommend ignoring the above, and starting again.

It seems that you are toggling the visibility of pairs of elements. There is no need to go through all the thousands of elements hiding elements that are already hidden. The better - and in cases like this, only sensible - toggling approach is to store the currently visible elements in a couple of global variables. When you want to switch to a new pair, just hide the elements held in the variables, show your new pair, then put them into your storage variables. It will take no time at all.