2

Got a form with a div dropdown which when select will display/hide another div.

<div>
    <select id="currency" name="currency" onChange="display_payroll_div(this.selectedIndex);">
        <option value="0">Currency for your quote*</option>
        <option value="AUD">AUD</option>
        <option value="USD">USD</option>
        <option value="Pounds">GBP</option>
    </select>
</div>
<div id="payroll-div">
    <select id="report-payroll" name="report-payroll">
        <option value="0">Would you like us to do payroll?*</option>
        <option value="1">Yes, I would like payroll</option>
        <option value="2">No, I don't need payroll</option>
    </select>
</div>

Javascript:

function display_payroll_div(e){
    currency_value=document.getElementById('currency').value;
    document.getElementById('payroll-div').style.display = "none";
    if (currency_value == 'AUD') {
        document.getElementById('payroll-div').style.display = "block";
    }
}

The display/hide is fine but the issue I have is when I select 'AUD' and 'Yes, I would like payroll' and then change my mind and change to 'USD' the hidden field payroll will still keep the value of 'Yes,...'. Is there anyway to reset 'payroll-div' to default value?

All suggestions I've seen uses 'onClick' which I'm trying to avoid as it does not work in FF. Any help would be appreciated :)

Manwal
  • 22,994
  • 11
  • 59
  • 91
Angie
  • 125
  • 1
  • 10
  • 1
    You should use onchange anyway. But create a jsfiddle.net so we do not have to. Do you mean `document.getElementById("report-payroll").selectedIndex=0` when hiding? – mplungjan Aug 28 '14 at 06:33

5 Answers5

1

Try this:

function display_payroll_div(e){
    currency_value=document.getElementById('currency').value;
    document.getElementById('payroll-div').style.display = "none";
    if (currency_value == 'AUD') {
        document.getElementById('payroll-div').style.display = "block";
    }
    else {
        document.getElementById('report-payroll').selectedIndex = 0;
    }
}

Working jsFiddle Demo.

dashtinejad
  • 6,127
  • 3
  • 26
  • 43
1

Reference: Reset the Value of a Select Box

add this in your code:

else
{
 document.getElementById('report-payroll').selectedIndex = 0;
 //reseting value when currency_value != 'AUD'
}

DEMO

Community
  • 1
  • 1
Manwal
  • 22,994
  • 11
  • 59
  • 91
1

Change the function like below

  function display_payroll_div(e){
      currency_value=document.getElementById('currency').value;
      document.getElementById('payroll-div').style.display = "none";
       if (currency_value == 'AUD') {
              document.getElementById('payroll-div').style.display = "block";
        } else {
        document.getElementById('report-payroll').value ='0';
      }
   }

JSFiddle Demo

1

You can use

document.getElementById('report-payroll').options[0].selected = true;
Oleksandr T.
  • 73,526
  • 17
  • 164
  • 143
0

I suggest this:

JSFIDDLE

1) unobtrusive
2) can handle a reload

<select id="currency" name="currency">

using

window.onload=function() {
  document.getElementById("currency").onchange=function(){
    var currency_value = this.value, 
        payrollDiv     = document.getElementById('payroll-div');
    payrollDiv.style.display = currency_value=="AUD"?"block":"none";
    if (currency_value!="AUD") document.getElementById("report-payroll").selectedIndex=0;
  }
  document.getElementById("currency").onchange(); // hide or show when loading
}
mplungjan
  • 155,085
  • 27
  • 166
  • 222