currently i am building a website that has a province/state drop down list, how this list work is that when I select canada/USA, this province/state list show up with proper province or state in the list, if i choose other country, nothing the list is hidden. In order to use it, javascript has to be enabled.
Right now I am trying to get this work without javascript enabled, i heard that i can use prototype.js to make it happen, but i am not familiar with this framework, can anyone give me help. i post the code below:
<script type="text/javascript" src="[% c.uri_for("/static/js/prototype.js") %]"></script>
<script type="text/javascript" src="[% c.uri_for("/static/js/effects.js") %]"></script>
<script type="text/javascript" src="[% c.uri_for("/static/js/controls.js") %]"></script>
<script type="text/javascript">
function countryChosen (country) {
country = $(country);
var subcountries = document.getElementsByClassName('subcountry');
for (i = 0; i < subcountries.length; i++) {
Element.hide(subcountries);
subcountries.getElementsByTagName("select")[0].disabled = true;
}
var subcountry = $("country_"+country.value+"_subcountries");
if (subcountry) {
Element.show(subcountry);
subcountryChosen(subcountry);
subcountry.getElementsByTagName("select")[0].disabled = false;
}
else {
subcountryChosen(null);
}
}
function subcountryChosen (subcountry) {
subcountry = $(subcountry);
}
Event.observe( window, 'load',
function () {
Event.observe( 'country', 'click', function () { countryChosen('country'); } );
Event.observe( 'country', 'keypress', function () { countryChosen('country'); } );
countryChosen( 'country' );
}
);
</script>
<div class="country" style="">
<label for="country">[% loc('Country') %] :</label>
<select name ="country" id= "country">
<option value="[% loc('') %]"></option>
[%
FOREACH country IN countries
%]
<option value="[% country.country_id %]"> [% country.name %]</option>
[%
END
%]
</select>
</div>
[%-CALL countries.reset; %]
[% FOREACH country IN countries %]
[% IF( subcountries = country.subcountries(lang = c.language) ); %]
<div class="subcountry" style="display: none; float: none;" id="country_[% country.country_id %]_subcountries">
<br>
<label for="subcountry">[% loc('Province/State') %]
[% FOREACH category IN country.sub_country_categories %]
[% category %]
[% UNLESS loop.last ; %]/ [% END %]
[% END %] :
</label>
<select onclick="subcountryChosen(this);" name="subcountry">
<option value=""></option>
[%
FOREACH subcountry IN subcountries
%]
<option value = "[% subcountry.subcountry_id | html %]"> [% subcountry.name | html %]</option>
[%
END
%]
</select>
</div>
[% END %]
[% END %]
Thank you
Right now I am trying to get this work without javascript enabled, i heard that i can use prototype.js to make it happen, but i am not familiar with this framework, can anyone give me help. i post the code below:
<script type="text/javascript" src="[% c.uri_for("/static/js/prototype.js") %]"></script>
<script type="text/javascript" src="[% c.uri_for("/static/js/effects.js") %]"></script>
<script type="text/javascript" src="[% c.uri_for("/static/js/controls.js") %]"></script>
<script type="text/javascript">
function countryChosen (country) {
country = $(country);
var subcountries = document.getElementsByClassName('subcountry');
for (i = 0; i < subcountries.length; i++) {
Element.hide(subcountries);
subcountries.getElementsByTagName("select")[0].disabled = true;
}
var subcountry = $("country_"+country.value+"_subcountries");
if (subcountry) {
Element.show(subcountry);
subcountryChosen(subcountry);
subcountry.getElementsByTagName("select")[0].disabled = false;
}
else {
subcountryChosen(null);
}
}
function subcountryChosen (subcountry) {
subcountry = $(subcountry);
}
Event.observe( window, 'load',
function () {
Event.observe( 'country', 'click', function () { countryChosen('country'); } );
Event.observe( 'country', 'keypress', function () { countryChosen('country'); } );
countryChosen( 'country' );
}
);
</script>
<div class="country" style="">
<label for="country">[% loc('Country') %] :</label>
<select name ="country" id= "country">
<option value="[% loc('') %]"></option>
[%
FOREACH country IN countries
%]
<option value="[% country.country_id %]"> [% country.name %]</option>
[%
END
%]
</select>
</div>
[%-CALL countries.reset; %]
[% FOREACH country IN countries %]
[% IF( subcountries = country.subcountries(lang = c.language) ); %]
<div class="subcountry" style="display: none; float: none;" id="country_[% country.country_id %]_subcountries">
<br>
<label for="subcountry">[% loc('Province/State') %]
[% FOREACH category IN country.sub_country_categories %]
[% category %]
[% UNLESS loop.last ; %]/ [% END %]
[% END %] :
</label>
<select onclick="subcountryChosen(this);" name="subcountry">
<option value=""></option>
[%
FOREACH subcountry IN subcountries
%]
<option value = "[% subcountry.subcountry_id | html %]"> [% subcountry.name | html %]</option>
[%
END
%]
</select>
</div>
[% END %]
[% END %]
Thank you