开发者

Swap the contents of 2 JavaScript variables from one to the other

开发者 https://www.devze.com 2023-01-18 12:50 出处:网络
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 buttonwhich swaps the variab

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;


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号