views:

68

answers:

2

Basically what I am wanting to do is take the variable named FROM and swap it with the variable named TO and vice visa, the reason for this is to allow the user to press a button which swaps the variables over when pressed. Exactly how it does when you press the swap button on google translate. Below is the code for the variables ect, but I have no idea on how to code the button so they swap over so to speak.

  function save_options_from() {
  var select = document.getElementById("FROM");
  var FROM = select.children[select.selectedIndex].value;
  localStorage["default_currency"] = FROM;

var
}

  function save_options_to() {
  var select = document.getElementById("TO");
  var TO = select.children[select.selectedIndex].value;
  localStorage["default_currency_to"] = TO;
+2  A: 

The FROM and TO variables are local to the two functions and don't exist at the same time. I think what you want is this:

var originalDefault = localStorage['default_currency'];
localStorage['default_currency'] = localStorage['default_currency_to'];
localStorage['default_currency_to'] = originalDefault;
Chuck
this kind of works, although 'default_currency_to' resets as if there was noting in storage, but the 'defaultCurrency' does change to the value that is in default_currency_to
Chris
@Chris: I messed up my naming conventions. The `localStorage` made my brain want to write `'defaultCurrency'`, but it should have been `'default_currency'`, which is the key you assigned. I've corrected it it now.
Chuck
Sorted, job done... just looked at the code corrected ['defaultCurrency'];... but thank you sooo much!!
Chris
A: 

I would suggest two hidden fields FROM_old and TO_old for doing the swapping, as you need to capture the value before you change it.

cfEngineers