-1

oke i have select field

<select id="select" required name="bank" >
  <option value="cash">Cash</option>
  <option value="debit">Debit Card</option>
  <option value="cc">Credit Card</option>
</select>

and the text field to show price

<input type="text" id="sub_total" name="sub_total">
<input type="text" id="fee" name="fee">
<input type="text" id="sum" name="total">

and the javascript

var total = 0;
var fees = 0;
var total_fees = fees + total;

$("#sub_total").val(total);
$("#fee").val(fees);
$("#sum").val(total_fees);

so the point is i want to change the "fees" value from "0" to "0.1 or what ever i want" if select credit card

the pseudecode is

if select cc var fees = '0.1'; else var fees = '0';

derylihs
  • 29
  • 1
  • 8
  • Possible duplicate of [jquery select change event get selected option](https://stackoverflow.com/questions/12750307/jquery-select-change-event-get-selected-option) – derylihs Aug 19 '19 at 19:29

2 Answers2

1
    $('#select').change(function() {
      if($(this).val() == "cc")
      {
         $('#fee').val(0.1);
      }
   });
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
0

Use a ternary operator to switch between 0 and .1 based on the select value

var fees = ($("#select").val() === "cc" ? 0.1 : 0);

You should wrap this in a function, and bind the select element to this function on change.

e.g. :

var sel = $("#select");

function setValue() {
  var total = 0,
      fees = (sel.val() === "cc" ? 0.1 : 0); // ternary

  $("#sub_total").val(total);
  $("#fee").val(fees);
  $("#sum").val(fees + total); // sum
}

setValue(); // call function
sel.bind('change', setValue);  // bind function to onchange of the select element
Jay Harris
  • 4,201
  • 17
  • 21