JavaScript alert statement makes code work; removing alert, code errors out

I have some JavaScript code that only works when the alert statements are used/not hidden…

I hide or remark them out, and the code errors out with “‘options.length’ is null or not an object” — I am guessing it is line #27 where this is happening…

Here is the code snippet:

  1. function editQuery(myForm) {

  2. var f;

  3. var x;

  4. // Get the row filters that were used in the last query…

  5. for (f = 1; f < 16; f++) {

  6. var filter = element(“FilterList_” + f);

  7. if (filter.selectedIndex > 0) {

  8. var methodElement = element(“FilterMethod_” + f);

  9. var methodIndex = methodElement.selectedIndex;

  10. //alert("methodIndex: " + methodIndex);

  11. var savedFilterMethodValue = methodElement.options[methodIndex].text;

  12. var choicesElement = element(“FilterChoices_” + f);

  13. var choicesIndex = choicesElement.selectedIndex;

  14. //alert("choicesIndex: " + choicesIndex);

  15. //alert("isNaN(choicesIndex): " + isNaN(choicesIndex));

  16. if (isNaN(choicesIndex)) {

  17. var savedFitlerValues = choicesElement.value;

  18. }

  19. else {

  20. var savedFitlerValues = choicesElement.options[choicesIndex].text;

  21. }

  22. updateFilters(filter); // update the filters

  23. // take the saved methods and values and then update the selections

  24. // Since the object was updated, get the object again…

  25. //alert("savedFilterMethodValue: " + savedFilterMethodValue);

  26. var methodElement = element(“FilterMethod_” + f);

  27. for (x = 0; x < methodElement.options.length; x++) {

  28. //alert("x: " + x);

  29. //alert("methodElement.options.text: " + methodElement.options.text);

  30. if (methodElement.options.text == savedFilterMethodValue) {

  31. methodElement.options.selected = true;

  32. //alert(“Found match - out…”);

  33. break;

  34. }

  35. }

  36. // Since the object was updated, get the object again…

  37. //alert("savedFitlerValues: " + savedFitlerValues);

  38. var choicesElement = element(“FilterChoices_” + f);

  39. for (x = 0; x < choicesElement.options.length; x++) {

  40. //alert("x: " + x);

  41. //alert("choicesElement.options.text: " + choicesElement.options.text);

  42. if (choicesElement.options.text == savedFitlerValues) {

  43. choicesElement.options.selected = true;

  44. //alert(“Found match - out…”);

  45. break;

  46. }

  47. }

  48. // Only display next row if f = 2…

  49. // If only one row was used, no reason display the next row…

  50. if (f == 2) {

  51. displayNextFilter(f - 1); // display it

  52. }

  53. }

  54. }

  55. }

I showed the alert on line 25 (all others are hidden) and it works fine, I hide it, it errors out…

Never had this happen…

Any ideas would be great — not much help in the JavaScript forums I belong to…

Thanks!

Dan

Subject: Just realized the answer…

Use the setTimeout function, so the HTML can “catch up”…

Thanks!

Dan