[thelist] Javascript: onChange fires every time the page loads

Iain Gardiner iain at firelightning.com
Thu Apr 21 11:31:47 CDT 2005


Hi guys,

Warning!  JS newbie coming through.  I have a problem with attaching 
onchange handlers to menu options.  The full script I am referring to is 
at http://www.firelightning.com/js/switcher.js and the specific function 
is this:

/* FUNCTION: ATTACH ONCHANGE EVENT TO MENU */
function menuSetup() {
	var styleMenu = document.getElementById('styleMenu');
	// Attach an onchange event to each menu option:
	for (var i=0; i < styleMenu.length; i++) {
		styleMenu[i].onchange = setStyle(styleMenu[i].value);
	}
}

Now, this function is called on page load and as far as I can tell this 
should work by just telling the browser to listen for the onchange 
event.  What actually happens is that when the page loads, the event is 
fired for each menu option in turn instead of waiting for user input.  I 
can't for the life of me work out why.  I don't see what fundamental 
difference there is between my script and the examples at 
http://www.quirksmode.org/js/events/window.html on PPK's site.

I really hope someone can help.  :)

Thanks,

Iain



More information about the thelist mailing list