Jeff P
Jeff P

Reputation: 51

Set dropdown value equal to hidden field

I have an html5 page where I want to set the value of a dropdown menu based on a hidden field but its not working.

function checkPayAdd() { 
var i;
var PayAddOption =document.querySelectorAll('input[name="PayAddOption"]');

for ( i = 0;  i < PayAddOption.length; i++) {

if (PayAddOption[i].checked == true) {

switch(i)
{
  case 0:
  document.getElementById("BillingAddress1").value = document.getElementById("Contact_Address1").value;      
  document.getElementById("BillingAddress2").value = document.getElementById("Contact_Address2").value;
  document.getElementById("BillingCity").value = document.getElementById("Contact_City").value;
  document.getElementById("BillingProv").selectedIndex = document.getElementById("First_Corp_Director_Prov").value;      
  document.getElementById("BillingPostalCode").value = document.getElementById("Contact_Postal").value; 
  document.getElementById("BillingCountry").selectedIndex = document.getElementById("First_Corp_Director_Country").value;       
  break     

I set the values of the hidden field in the same format as those in the drop downs - ie. Province value in hidden field is: "AL" which should then set the Billing.Prov dropdown value to the same. These are the hidden fields

<input type="hidden" name="First_Corp_Director_Province"  id="First_Corp_Director_Province" value="AL">

        <input type="hidden" name="First_Corp_Director_Country" id="First_Corp_Director_Country" value="US">

<select name="BillingProv" id="BillingProv" class="form-control">
            <option value="">select State or Province (Canada/US Only)</option>
            <option value="ZZ">Outside US or Canada</option>
            <optgroup label="United States">
              <option id="USA-AL" value="AL">Alabama (AL)</option>
              <option id="USA-AK" value="AK">Alaska (AK)</option>
              <option id="USA-AZ" value="AZ">Arizona (AZ)</option> 

Any help would be appreciated!

Upvotes: 1

Views: 1976

Answers (1)

Kishore Barik
Kishore Barik

Reputation: 770

There are 2 errors here

  1. You should be assigning to the value of the select element instead of selectedIndex.

  2. The id of hidden field is wrong. Should be First_Corp_Director_Province instead of First_Corp_Director_Prov.

so the replace your 4th line of switch case 0 with

document.getElementById("BillingProv").value = document.getElementById("First_Corp_Director_Province").value;

and 6th line with

document.getElementById("BillingCountry").value = document.getElementById("First_Corp_Director_Country").value; 

Upvotes: 2

Related Questions