Well, first, i'm not sure what your "onchange(country)" function does. It would make more sense to use Prototype's built in "$F" function to find the value of the options. To start, change your options from
Code:
<option>Alberta</option>
to
Code:
<option value="Alberta">Alberta</option>
then try this
Code:
Event.observe("country", "change"; function(e) {
if ($F(country) == "Canada") {
$("province-canada").show();
}
else {
$("province-canada").hide();
}
if ($F(country) == "United States") {
$("province-us").show();
}
else {
$("province-us").hide();
}
});
I'm not sure if the Prototype guys changed their design, but $ Function didn't used to attach Functions to the element. Before v. 1.5.1 you need to do
Code:
Element.show("province-canada");
and
Code:
Element.hide("province-canada");
If you are going to have a lot of hidden province elements, you will probably want to re-architect your code without a ton of if/else statements. If you attach a class to the hidden selects, like 'hidden' that has the style 'display:none', and use another class list 'show' to override the display. Then you can use document.getElementByClassName('show', parentElement); to quickly find the currently visible select and hide it. Then you just need to use a good naming schema to map the selected country to the province id to grab the select to show.