I need help with an assignment. Don't need the answer, just what is wrong with my code.
I need to create a init() function. Create a variable named allSelect that references elements in my document. For each item in the allSelect object collection, add an onchange event handler that runs loadLink() function.
Create loadLink() function. Create a variable sIndex that points to the index of the selection list. Then loads the web page requested.

<script type="text/javascript">

window.onload = init;
var allSelect;
var sIndex;

function init() {
   var allSelect = document.getElementsByTagName("*");
      
   for (var i = 0; i < allselect.length; i++) {
      if (allselect[i].className == "optionLinks") allSelect.push(allSelect[i]);
      
      }
   }

allSelect[i].onchange = loadLink;

function loadLink(siteList) {
   var sIndex = this.id + "optionLinks";
   location.href = siteList.options[sIndex].value;
   }

</script>

when you use var BEFORE your variable, it make that variable "private". In other words, it will NOT be visible outside your function. Thus, the variable allSelect in line 4 is NOT the same as the one in line 8. The allSelect variable declared in line 8 exists ONLY WITHIN that function. As soon as init() finishes executing, that variable is NOT accessible from outside. So, on line 8 you need to get rid of "var" so that your function populates the global variable (on line 4).

Also, line 16 is NOT correct. Why? Because init() will be called onload. In other words, WHILE the page is still loading, lines 7-14 will be ignored. The javascript interpreter will then try to execute line 16, but allSelect is still NOT an array. There isn't even a variable named i because:
a. i exists only within init() since it is declared with var b. even if you were to get rid of var , init() has NOT executed yet!

So you need to execute that line WITHIN the for loop in the init() function.

Thank you for your help. I made those changes, but it still doesn't work. Is there something wrong with the way I am trying to get it to go to a specific URL when that selection is made?

assuming that allSelect is meant to be an array of references to all the SELECT elements, then you should be explicitly selecting/getting only the SELECT elements:

...
function init() {

  allSelect = document.getElementsByTagName("SELECT");
...
}

as for your loadLinks, try the following to see if you get the expected/selected value.

function loadLinks()
{
 if( this.selectedIndex > -1 )
  alert( this.options[this.selectedIndex].value )
 else
  alert('Nothing selected')
}

If you are still having problems, then post your updated code.

Thank you so much for all your help. I got it now.

Hi..sorry to jump in the middle here but I am working the same problem. I was able to progress forward some from what you two have been saying but I have been unable to work the code correctly. Below is the code I have written so far...and it looks similar to what you guys started with. Any help would be appreciated.

25 window.onload = init;
26 var sIndex
27
28 function init() {
29 allSelect = document.getElementsByTagName("select");
30
31 for (var i = 0; i < allSelect.length; i++) {
32 if (allSelect.className == "optionLinks")
33 allSelect.push(allSelect);
34 }
35 allSelect.onchange = loadLink;
36 }
37
38 function loadLink(siteList) {
39 if (this.selectedIndex > -1)
40 alert(this.options[this.selectedIndex].value)
41 else
42 alert('Nothing selected')
43 }

Thanks for any help you can provide..I've been working on this off and on for couple days and I am not sure I am getting it.

if (allSelect.className == "optionLinks")
allSelect.push(allSelect);

No. IF allSelect originally has TWO SELECT elements - due to getElementsByTagName() - and one of them has a class="optionLinks" , then what you are doing is taking that same SELECT (the one with class="optionLinks") and adding ANOTHER reference to it at the end of allSelect. Thus allSelect now has three elements. Eventually your loop will check this new added element and see that it TOO has class="optionLinks" and another reference to it at the end of allSelect. Thus, will keep doing this infinitely.

To fix your problem, all you have to do is REPLACE line 33 with line 35.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.