0

I have a select option, also have a div with a span inside:

The goal is when change the option of the select option, the span 's value will change. Here's how i came up with:

$(document).ready(function() {
  var money_span = $("#amount_of_money");
  var money_value = money_span.val();
  var selected = $('#Bank_Type123 option:selected').val();

  $('#Bank_Type123').change(function(event) {
    event.preventDefault();
    money = 0;
    if (selected === "Mizuho") {
      money = 10;
    } else if (selected === "UFJ") {
      money = 11;
    } else if (selected === "Yucho") {
      money = 12;
    }
    return money;
  });
  money_value = money;
  money_span.append(money_value);
  money_span.show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Bank_Type123" class="form-control">
     <option value="" disabled selected style="display:none;">Choose A Bank</option>
     <option value="Mizuho">Mizuho</option>
     <option value="UFJ" >UFJ</option>
     <option value="Yucho" >Yucho</option>
</select>

<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>

But when I try, nothing appear in the span when changed the option in select menu.

Pedram
  • 15,766
  • 10
  • 44
  • 73
Hoàng Việt
  • 174
  • 1
  • 14
  • callback on event change is asynchronous. So when you are trying to assign money_value from money it will get undefined value. – Diptopol Dam Dec 19 '17 at 07:53

7 Answers7

2

Hi took the liberty of changing it like this

HTML

<select id="Bank_Type123" class="form-control">
  <option value="" disabled selected style="display:none;">Choose A Bank</option>
  <option value="10">Mizuho</option>
  <option value="11">UFJ</option>
  <option value="12">Yucho</option>
</select>

<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>

JS

$(document).ready(function() {
  $('#Bank_Type123').change(function(event) {
    $("#amount_of_money").text($(this).val());
  });
});

I feel instead of hardcoding the values in JS it would be good to specify it in select option itself.

Working Fiddle

Sunil Hari
  • 1,716
  • 1
  • 12
  • 20
1

You have a lot of errors here:

  1. somehow you forget to track "selected" item change (it should be moved to "change"-event handler)
  2. Same (not to mention you forgot to define "money" variable), it also should be set in event handler
  3. ".append" will add content to existing, but I believe you need to replace it, so use ".html" instead

here is how it should work (if I understand correctly from your code-samples):

$(document).ready(function () {
    var money_span = $("#amount_of_money"),
            money_value = money_span.val(),
            money = '',
            selected = $('#Bank_Type123 option:selected').val();

    $('#Bank_Type123').change(function (event) {
        event.preventDefault();
        selected = $('#Bank_Type123 option:selected').val();
        money = 0;
        if (selected === "Mizuho") {
            money = 10;
        } else if (selected === "UFJ") {
            money = 11;
        }  else if(selected === "Yucho") {
        money = 12;
        }
        money_value = money;
        money_span.html(money_value);
        money_span.show();
    });
});
2oppin
  • 1,941
  • 20
  • 33
  • Thanks for full explaination with example . However , I think the "money" and "selected" global variable is not necessary – Hoàng Việt Dec 19 '17 at 08:11
  • @HoàngViệt, maybe, but if you enclose it in event-handler, you will be lack of flexibility to upgrade functionality, if something other can change that variable, or you will need to keep track it. besides it's "lesser changes" to let author to decide ). – 2oppin Dec 19 '17 at 08:22
0

$(document).ready(function() {
  var money_span = $("#amount_of_money");
  var money_value = money_span.val();
  

  $('#Bank_Type123').change(function(event) {
    var selected = $('#Bank_Type123 option:selected').val();
    event.preventDefault();
    money = 0;
    if (selected === "Mizuho") {
      money = 10;
    } else if (selected === "UFJ") {
      money = 11;
    } else if (selected === "Yucho") {
      money = 12;
    }
    money_value = money;
    money_span.text('');
  money_span.append(money_value);
  money_span.show();
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Bank_Type123" class="form-control">
     <option value="" disabled selected style="display:none;">Choose A Bank</option>
     <option value="Mizuho">Mizuho</option>
     <option value="UFJ" >UFJ</option>
     <option value="Yucho" >Yucho</option>
</select>

<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>

Move these thre lines inside of the change event

money_value = money;
        money_span.append(money_value);
        money_span.show();
Lalit Sachdeva
  • 6,469
  • 2
  • 19
  • 25
0

I think you are looking for this https://jsfiddle.net/xkfqddwc/

$(document).ready(function () {
  var money_span = $("#amount_of_money");
  var selected ='';
  var money = '';

  $('#Bank_Type123').change(function (event) {
      event.preventDefault();
      debugger;
      selected = $(event.target).val();
      money = 0;
      if (selected === "Mizuho") {
          money = 10;
      } else if (selected === "UFJ") {
          money = 11;
      }  else if(selected === "Yucho") {
          money = 12;
      }

    money_span.html(money);
    money_span.show();
  });
});
Aswin Ramesh
  • 1,654
  • 1
  • 13
  • 13
0

A shorter version can be like this:

var money_span = $("#amount_of_money");

$('#Bank_Type123').change(function(event) {
  event.preventDefault();
  var selected = $(this).val();

  if (selected === "Mizuho") {
    money_span.text(10);
  } else if (selected === "UFJ") {
    money_span.text(11);
  } else if (selected === "Yucho") {
    money_span.text(12);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Bank_Type123" class="form-control">
     <option value="" disabled selected style="display:none;">Choose A Bank</option>
     <option value="Mizuho">Mizuho</option>
     <option value="UFJ" >UFJ</option>
     <option value="Yucho" >Yucho</option>
</select>

<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>
Pedram
  • 15,766
  • 10
  • 44
  • 73
0

Put $('#Bank_Type123 option:selected').val(); inside the change event like below:

$(document).ready(function() {
  var money_span = $("#amount_of_money");
  $('#Bank_Type123').change(function(event) {
    var selected = $('#Bank_Type123 option:selected').val();
    if (selected === "Mizuho") {
      money_span.text(10);
    } else if (selected === "UFJ") {
      money_span.text(11);
    } else if (selected === "Yucho") {
      money_span.text(12);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Bank_Type123" class="form-control">
     <option value="" disabled selected style="display:none;">Choose A Bank</option>
     <option value="Mizuho">Mizuho</option>
     <option value="UFJ" >UFJ</option>
     <option value="Yucho" >Yucho</option>
</select>
<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
0

$(document).ready(function(){
$('#Bank_Type123').change(function(){
$('#amount_of_money').text($('option:selected').val())
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Bank_Type123" class="form-control">
  <option value="" disabled selected style="display:none;">Choose A Bank</option>
  <option value="10">Mizuho</option>
  <option value="11">UFJ</option>
  <option value="12">Yucho</option>
</select>

<div class="input-group px-1 py-1">
  <span class="input-group-addon">Wallet</span>
  <span id="amount_of_money" class="input-group-addon unique"></span>
  <span class="input-group-addon">JPY</span>
</div>
vicky patel
  • 699
  • 2
  • 8
  • 14