Menu
Hi All,
I have basic JavaScript that pre-populates the state field based on Country selection. Can an expert take a look? It was working a while back but now it’s not. Not really sure why.
Also, can you also look at the JavaScript that copies the shipping information into the billing information section? All of it works except for the State field.
Please help! I have attached the full code as a text file.
[CODE]
[COLOR="Blue"]// A simple comma (or |) separated string and the corresponding array for Usa[/COLOR]
var strSttUsa = "Alabama|AB|Alaska|AK|Arizona|AZ|Arkansas|AR|California|CA|Colorado|CO|Connecticut|CT|Delaware|DE|District of Columbia|DC|Florida|FL|Georgia|GA|Hawaii|HI|Idaho|ID|Illinois|IL|Indiana|IN|Iowa|IA|Kansas|KS|Kentucky|KY|Louisiana|LA|Maine|ME|Maryland|MD|Massachusetts|MA|Michigan|MI|Minnesota|MN|Mississippi|MS|Missouri|MO|Montana|MT|Nebraska|NE|Nevada|NV|New Hampshire|NH|New Jersey|NJ|New Mexico|NM|New York|NY|North Carolina|NC|North Dakota|ND|Ohio|OH|Oklahoma|OK|Oregon|OR|Pennsylvania|PA|Puerto Rico|PR|Rhode Island|RI|South Carolina|SC|South Dakota|SD|Tennessee|TN|Texas|TX|Utah|UT|Vermont|VT|Virginia|VA|Washington|WA|West Virginia|WV|Wisconsin|WI|Wyoming|WY";
var arrSttUsa = strSttUsa.split('|');[COLOR="Blue"]
// Same string and array for Canada[/COLOR]
var strSttCan = "Alberta|AB|British Columbia|BC|Manitoba|MB|New Brunswick|NB|New Foundland|NL|NorthWest Territories|NT|Nova Scotia|NS|Ontario|ON|Prince Edward Island|PE|Quebec|QC|Saskatchewan|SK|Yukon|YT|";
var arrSttCan = strSttCan.split('|');
[COLOR="Blue"]// Same things for countries with a unique value (to do for a lighter file)[/COLOR]
[COLOR="Blue"]// An unique function to call with each list with different arguments[/COLOR]
function changeStates(objOrgLst,idDstLst){
[COLOR="Blue"]// A syntax to work without the values[/COLOR] (usual syntax : var country = objOrgLst.value)
var country = objOrgLst.options[objOrgLst.options.selectedIndex].innerHTML;//alert(country)
var slcLst = document.getElementById(idDstLst);//alert(slcLst)
if (country=="United States"){
slcLst.options.length=0;
slcLst.options[0] = new Option('--- Select One ---','');
for (var l=arrSttUsa.length,i=0;i<l;[COLOR="Red"]i+=2[/COLOR]) slcLst.options[1+i/2] = new Option(arrSttUsa[i],arrSttUsa[i+1]);}
else if (country=="Canada"){
slcLst.options.length=0;
slcLst.options[0] = new Option('--- Select One ---','');
for (var l=arrSttCan.length,i=0;i<l;i[COLOR="Red"]+=2[/COLOR]) slcLst.options[1+i/2] = new Option(arrSttCan[i],arrSttCan[i+1]);}
else if (country!="Canada" && country!="United States"){
slcLst.options.length=0;
slcLst.options[0] = new Option('Not Applicable','Not Applicable');}
}[/CODE]
Then to call this function you have only to display two onchange on the two lists[CODE]
// First list
<select id="Shipping_Country" name="Shipping_Country" [COLOR="Red"]onchange="changeStates(this,'Shipping_State')"[/COLOR]>
<option selected="selected">--- Select One ---</option>
<option value="Afghanistan">Afghanistan</option>
<!-- -->
<select id="Billing_Country" name="Billing_Country" [COLOR="Red"]onchange="changeStates(this,'Billing_State')"[/COLOR]>
<option selected="selected">--- Select One ---</option>
<option value="Afghanistan">Afghanistan</option>
<!-- -->
[/CODE]
Good Luck ![CODE]
if (country=="United States"){
//...
}
else if (country=="Canada"){
//...
}
else [COLOR="Blue"]// test to remove : if (country!="Canada" && country!="United States")[/COLOR]
{
//...
}
[/CODE]
[CODE]function SetBilling(checked){
if (checked){
changeStates(document.getElementById('Shipping_Country'),'Billing_State');
[COLOR="Blue"]var frmElm = document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++) if (frmElm[i].id.substr(0,8)=="Shipping") [/COLOR]{
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = frmElm[i].value;}
}
else{
document.getElementById('Billing_Address_1').value = '';
document.getElementById('Billing_Address_2').value = '';
document.getElementById('Billing_City').value = '';
document.getElementById('Billing_State').value = '';
document.getElementById('Billing_Postal').value = '';
document.getElementById('Billing_Country').value = '';
}
}[/CODE]
But it would be probably easier for the user to copy each Billing elements with an onblur of the corresponding Shipping element and to call twice the changeStates function... [CODE]<select id="Shipping_Country" name="Shipping_Country" onchange="[COLOR="Blue"]changeStates(this,'Shipping_State');changeStates(this,'Billing_State')[/COLOR]">[/CODE]
Then the user will not have more than to make the necessary modifications. [CODE]<script type="text/javascript">
function SetBilling(checked) {
if (checked) {
document.getElementById('Billing_First_Name').value = document.getElementById('Shipping_First_Name').value;
document.getElementById('Billing_Last_Name').value = document.getElementById('Shipping_Last_Name').value;
document.getElementById('Billing_Company').value = document.getElementById('Shipping_Company').value;
document.getElementById('Billing_Address_1').value = document.getElementById('Shipping_Address_1').value;
document.getElementById('Billing_Address_2').value = document.getElementById('Shipping_Address_2').value;
document.getElementById('Billing_Country').value = document.getElementById('Shipping_Country').value;
document.getElementById('Billing_City').value = document.getElementById('Shipping_City').value;
document.getElementById('Billing_Postal').value = document.getElementById('Shipping_Postal').value;
document.getElementById('Billing_Telephone').value = document.getElementById('Shipping_Telephone').value;
changeStates(document.getElementById('Shipping_Country'),'Billing_State');
var frmElm = document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++) if (frmElm[i].id.substr(0,8)=="Shipping") {
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = frmElm[i].value;}
} else {
document.getElementById('Billing_First_Name').value = '';
document.getElementById('Billing_Last_Name').value = '';
document.getElementById('Billing_Company').value = '';
document.getElementById('Billing_Address_1').value = '';
document.getElementById('Billing_Address_2').value = '';
document.getElementById('Billing_Country').selectedIndex = 0;
document.getElementById('Billing_City').value = '';
document.getElementById('Billing_State').selectedIndex = 0;
document.getElementById('Billing_Postal').value = '';
document.getElementById('Billing_Telephone').value = '';
}
}
</script>[/CODE]
[CODE]<script type="text/javascript">
function SetBilling(checked) {
if (checked) {
changeStates(document.getElementById('Shipping_Country'),'Billing_State');
var frmElm = document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++) if (frmElm[i].id.substr(0,8)=="Shipping") {
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = frmElm[i].value;}
} else {
document.getElementById('Billing_First_Name').value = '';
document.getElementById('Billing_Last_Name').value = '';
document.getElementById('Billing_Company').value = '';
document.getElementById('Billing_Address_1').value = '';
document.getElementById('Billing_Address_2').value = '';
document.getElementById('Billing_Country').selectedIndex = 0;
document.getElementById('Billing_City').value = '';
document.getElementById('Billing_State').selectedIndex = 0;
document.getElementById('Billing_Postal').value = '';
document.getElementById('Billing_Telephone').value = '';
}
}
</script>[/CODE]
[CODE]<script language="JavaScript" type="text/javascript">
var strSttUsa = "Alabama|AB|Alaska|AK|Arizona|AZ|Arkansas|AR|California|CA|Colorado|CO|Connecticut|CT|Delaware|DE|District of Columbia|DC|Florida|FL|Georgia|GA|Hawaii|HI|Idaho|ID|Illinois|IL|Indiana|IN|Iowa|IA|Kansas|KS|Kentucky|KY|Louisiana|LA|Maine|ME|Maryland|MD|Massachusetts|MA|Michigan|MI|Minnesota|MN|Mississippi|MS|Missouri|MO|Montana|MT|Nebraska|NE|Nevada|NV|New Hampshire|NH|New Jersey|NJ|New Mexico|NM|New York|NY|North Carolina|NC|North Dakota|ND|Ohio|OH|Oklahoma|OK|Oregon|OR|Pennsylvania|PA|Puerto Rico|PR|Rhode Island|RI|South Carolina|SC|South Dakota|SD|Tennessee|TN|Texas|TX|Utah|UT|Vermont|VT|Virginia|VA|Washington|WA|West Virginia|WV|Wisconsin|WI|Wyoming|WY"
var arrSttUsa = strSttUsa.split('|');
var strSttCan = "Alberta|AB|British Columbia|BC|Manitoba|MB|New Brunswick|NB|New Foundland|NL|NorthWest Territories|NT|Nova Scotia|NS|Ontario|ON|Prince Edward Island|PE|Quebec|QC|Saskatchewan|SK|Yukon|YT|";
var arrSttCan = strSttCan.split('|');
function changeStates(objOrgLst,idDstLst){var l,i,lstStt,nptStt,objParent;
var country = objOrgLst.options[objOrgLst.options.selectedIndex].innerHTML;
var objStt = document.getElementById(idDstLst);
if (country=="United States" || country=="Canada"){
[COLOR="Blue"]// A drop-dawn list[/COLOR]
if (objStt.nodeName!='SELECT') {objParent=objStt.parentNode;
lstStt=document.createElement('select');
lstStt.onchange=function(){
if (document.getElementById('sameListBtn').checked) {
document.getElementById('Billing_State').value=document.getElementById('Shipping_State').value}}
objParent.replaceChild(lstStt,objStt);
lstStt.id=idDstLst;lstStt.name=idDstLst;}
else lstStt=objStt;
[COLOR="Blue"]// with options[/COLOR]
lstStt.options.length=0;
lstStt.options[0] = new Option('--- Select One ---','one',true,true);
if (country=="United States"){
for (l=arrSttUsa.length,i=0;i<l;i+=2) lstStt.options[1+i/2] = new Option(arrSttUsa[i],arrSttUsa[i+1]);}
else if (country=="Canada"){
for (l=arrSttCan.length,i=0;i<l;i+=2) lstStt.options[1+i/2] = new Option(arrSttCan[i],arrSttCan[i+1]);}
[COLOR="Blue"]// Same thing for the Billing list if the button is checked[/COLOR]
if (document.getElementById('sameListBtn').checked && objOrgLst.id=='Shipping_Country') {
objStt = document.getElementById('Billing_State');
if (objStt.nodeName!='SELECT') {objParent=objStt.parentNode;
lstStt=document.createElement('select');
objParent.replaceChild(lstStt,objStt);
lstStt.id='Billing_State';lstStt.name='Billing_State';
}
else lstStt=objStt;
lstStt.options.length=0;
lstStt.options[0] = new Option('--- Select One ---','one',true,true);
if (country=="United States"){
for (l=arrSttUsa.length,i=0;i<l;i+=2) lstStt.options[1+i/2] = new Option(arrSttUsa[i],arrSttUsa[i+1]);}
else if (country=="Canada"){
for (l=arrSttCan.length,i=0;i<l;i+=2) lstStt.options[1+i/2] = new Option(arrSttCan[i],arrSttCan[i+1]);}
}
}
else {
[COLOR="Blue"]// An input [/COLOR]
if (objStt.nodeName!='INPUT') {objParent=objStt.parentNode;
var nptStt=document.createElement('input');
nptStt.type='text';nptStt.size=30;
nptStt.onchange=function(){
if (document.getElementById('sameListBtn').checked) {
document.getElementById('Billing_State').value=document.getElementById('Shipping_State').value}}
objParent.replaceChild(nptStt,objStt);
nptStt.name=idDstLst;nptStt.id=idDstLst;}
[COLOR="Blue"]// Same thing for the Billing list if the button is checked[/COLOR]
if (document.getElementById('sameListBtn').checked && objOrgLst.id=='Shipping_Country') {
objStt = document.getElementById('Billing_State');
if (objStt.nodeName!='INPUT') {objParent=objStt.parentNode;
var nptStt=document.createElement('input');
nptStt.type='text';nptStt.size=30;
objParent.replaceChild(nptStt,objStt);
nptStt.name='Billing_State';nptStt.id='Billing_State';}}}
[COLOR="Blue"]// update the country Billing value if the button is checked[/COLOR]
if (document.getElementById('sameListBtn').checked && objOrgLst.id=='Shipping_Country') {
document.getElementById('Billing_Country').value=document.getElementById('Shipping_Country').value;
}
}
function SetBilling(checked){var i,frmElm;
if (checked){
[COLOR="Blue"]// To copy the type and list of states[/COLOR]
changeStates(document.getElementById('Shipping_Country'),'Billing_State');
// To copy and attach an onchange event on each Shipping item
frmElm=document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++){
if (frmElm[i].id.substr(0,8)=="Shipping" && (frmElm[i].tagName=='SELECT' || frmElm[i].tagName=='INPUT')){
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = frmElm[i].value;
[COLOR="Blue"]// Do not change events for select items[/COLOR]
if (frmElm[i].tagName!='SELECT') frmElm[i].onchange=function(){
document.getElementById(this.id.replace(/Shipping/,'Billing')).value=this.value;}}}}
else {
[COLOR="Blue"]// To transform the list of states in an input[/COLOR]
var objStt = document.getElementById('Billing_State');
if (objStt.nodeName!='INPUT') {objParent=objStt.parentNode;
var nptStt=document.createElement('input');
nptStt.type='text';nptStt.size=30;
objParent.replaceChild(nptStt,objStt);
nptStt.name='Billing_State';nptStt.id='Billing_State';}
[COLOR="Blue"]// To remove onchange event on each Shipping item and value on each Billing item[/COLOR]
var frmElm=document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++) {
if (frmElm[i].id.substr(0,8)=="Shipping" && (frmElm[i].tagName!='SELECT')){
if (frmElm[i]!='Shipping_Country') frmElm[i].onchange=null;
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = ' ';}}
document.getElementById('Billing_Country').selectedIndex=0;
}
}
</script>[/CODE]
I add an id (sameListBtn) to the checkbox and replace [B]the two drop dawn lists[/B] for State by to input like this[CODE]<tr>
<td> </td>
<td>State: </td>
<td>
<input type="text" name="Shipping_State" id="Shipping_State" size="30">
[COLOR="SeaGreen"]<!--
<select name="Shipping_State" id="Shipping_State">
<option selected="selected">--- Select One --</option>
</select>
-->[/COLOR]
</td>
</tr>[/CODE]
[CODE]<html>
<head>
<script language="JavaScript">
function clearselect()
{
document.myform.State.options.length=0;
}
function changeStates()
{
var indx=document.myform.Country.selectedIndex
var Country=document.myform.Country.options[indx].value
if (Country=="United States")
{
clearselect();
document.myform.State.options[0] = new Option('--- Select One ---','');
document.myform.State.options[1] = new Option('Alabama','AB');
}
else if (Country=="Canada")
{
clearselect();
document.myform.State.options[0] = new Option('--- Select One ---','');
document.myform.State.options[1] = new Option('Alberta','AB');
}
else if (Country!="Canada" && Country!="United States")
{
clearselect();
document.myform.State.options[0] = new Option('Not Applicable','Not Applicable');
}
}
</script>
</head>
<body>
<form id="myform">
<table>
<tr>
<td>Country: </td>
<td><select name="Country" onChange="return changeStates()">
<option value="" selected>--- Select One--- </option>
<option value="Afghanistan">Afghanistan</option>
<option value="Canada">Canada</option>
<option value="United States">United States</option>
</select></td>
</tr>
<tr>
<td>State: </td>
<td><select name="State"></select></td>
</tr>
</table>
</form>
</body>
</html>
[/CODE]
[CODE]<script language="JavaScript" type="text/javascript">
[COLOR="Blue"]// List to be completed for federations[/COLOR]
var oListsOfStates={// the country string value for key, and for value, a | separated string with text and value for all the States
"United States":"Alabama|AB|Alaska|AK|Arizona|AZ|Arkansas|AR|California|CA|Colorado|CO|Connecticut|CT|Delaware|DE|District of Columbia|DC|Florida|FL|Georgia|GA|Hawaii|HI|Idaho|ID|Illinois|IL|Indiana|IN|Iowa|IA|Kansas|KS|Kentucky|KY|Louisiana|LA|Maine|ME|Maryland|MD|Massachusetts|MA|Michigan|MI|Minnesota|MN|Mississippi|MS|Missouri|MO|Montana|MT|Nebraska|NE|Nevada|NV|New Hampshire|NH|New Jersey|NJ|New Mexico|NM|New York|NY|North Carolina|NC|North Dakota|ND|Ohio|OH|Oklahoma|OK|Oregon|OR|Pennsylvania|PA|Puerto Rico|PR|Rhode Island|RI|South Carolina|SC|South Dakota|SD|Tennessee|TN|Texas|TX|Utah|UT|Vermont|VT|Virginia|VA|Washington|WA|West Virginia|WV|Wisconsin|WI|Wyoming|WY"
,"Canada":"Alberta|AB|British Columbia|BC|Manitoba|MB|New Brunswick|NB|New Foundland|NL|NorthWest Territories|NT|Nova Scotia|NS|Ontario|ON|Prince Edward Island|PE|Quebec|QC|Saskatchewan|SK|Yukon|YT|"
};
function toogleInputList(object,country){
/* Adapts if necessary the Shipping or Billing State object (input type or drop-dawn list for countries in oListsOfStates)
and remove the corresponding list.
For Shipping State:
- adapts (if sameListBtn checked) the Billing State Object (with a new call of tis function),
- attach an onchange event to make, if necessary, a copy to Billing State.
object, define the Shipping or the Billing State object,
country, the concerned country.
*/
var newObj,okList=typeof(oListsOfStates[country])!='undefined';
[COLOR="Blue"]// Adapts the object[/COLOR]
if (okList && object.nodeName!='SELECT' || !okList && object.nodeName!='INPUT'){
var idName=object.id;
if (okList && object.nodeName!='SELECT') newObj=document.createElement('select');
else {newObj=document.createElement('input');newObj.type='text';newObj.size=30;}
object.parentNode.replaceChild(newObj,object);
newObj.id=idName;newObj.name=idName;}
else newObj=object;
[COLOR="Blue"]// Remove the drop-dawn list[/COLOR]
if (newObj.nodeName=='SELECT'){
newObj.options.length=0;
newObj.options[0] = new Option('--- Select One ---','one',true,true);
var arrStt=oListsOfStates[country].split('|');
for (var l=arrStt.length,i=0;i<l;i+=2) newObj.options[1+i/2] = new Option(arrStt[i],arrStt[i+1])}
[COLOR="Blue"]// For Shipping : onchange copy (if sameListBtn) and now change the Billing State object[/COLOR]
if (/Shipping/.test(object.id)){
newObj.onchange=function(){
if (document.getElementById('sameListBtn').checked) {
document.getElementById('Billing_State').value=this.value}}
toogleInputList(document.getElementById('Billing_State'),country)}
}
function changeStates(currentList,idTargetElm){
[COLOR="Blue"]// Copy the country change if necessary[/COLOR]
if (currentList.id=='Shipping_Country' && document.getElementById('sameListBtn').checked) {
document.getElementById('Billing_Country').value=currentList.value;}
var targetElm = document.getElementById(idTargetElm);
var country = currentList.value;// currentList.options[current.options.selectedIndex].innerHTML (if no value)
toogleInputList(targetElm,country);
}
function SetBilling(checked){var i,frmElm;
if (checked){
[COLOR="Blue"]// Copy the Country and adapts the type and list of states[/COLOR]
changeStates(document.getElementById('Shipping_Country'),'Billing_State');
[COLOR="Blue"]// Copy and attach an onchange event on each Shipping item[/COLOR]
frmElm=document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++){
if (frmElm[i].id.substr(0,8)=="Shipping" && (frmElm[i].tagName=='SELECT' || frmElm[i].tagName=='INPUT')){
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = frmElm[i].value;
// Do not change events for select items
if (frmElm[i].tagName!='SELECT') frmElm[i].onchange=function(){
document.getElementById(this.id.replace(/Shipping/,'Billing')).value=this.value;}}}}
else {
[COLOR="Blue"]// Transform the list of states in an input[/COLOR]
toogleInputList(document.getElementById('Billing_State'),'');
[COLOR="Blue"]// Remove onchange event on each Shipping item and value on each Billing item, set Billing_Country to 0[/COLOR]
var frmElm=document.myform.elements;
for (var l=frmElm.length,i=0;i<l;i++){
if (frmElm[i].id.substr(0,8)=="Shipping" && (frmElm[i].tagName!='SELECT')){
if (frmElm[i]!='Shipping_Country') frmElm[i].onchange=null;
document.getElementById('Billing'+frmElm[i].id.substr(8)).value = ' ';}}
document.getElementById('Billing_Country').selectedIndex=0;}
}
[COLOR="Blue"]// Ensure the adaptation of lists and events[/COLOR]
window.onload=function(){document.myform.reset();}
</script>[/CODE]
[CODE]<input type="checkbox" name="sameListBtn" id="sameListBtn">[/CODE]
[CODE]<input type="text" name="Shipping_State" id="Shipping_State" size="30">
<!--
<select name="Shipping_State" id="Shipping_State">
<option selected="selected">--- Select One --</option>
</select>
-->[/CODE]
[CODE] <input type="text" name="Billing_State" id="Billing_State" size="30">
<!--
<select name="Shipping_State" id="Shipping_State">
<option selected="selected">--- Select One --</option>
</select>
-->[/CODE]
[CODE]
// The line 41
else {newObj=document.createElement('input');newObj.type='text';newObj.size=30;}
// Is to complete by
else {newObj=document.createElement('input');newObj.type='text';newObj.size=30;
newObj.value="Not applicable";newObj.readOnly=true;}
[/CODE]
0.1.9 — BETA 6.17