user1469
user1469

Reputation: 109

javascript get select option value AND hidden text field value for calculation

My form needs to perform that following calculation.

When a select menu option is chosen, the title attribute is successfully copied to a text field [textfield01].

I have another hidden text field [textfield02] who's value is determined by a php query (total count from db).

I have a third text field [textfield03] that I need to calculate based on the following condition. If [textfield02] > [textfield01] then the value of [textfield03] will = [textfield02]/2

The code I have working so far is:

jQuery(document).ready(function() {
  // pass the title of the selected topic option to a hidden 'topic' form field
  jQuery('select#textfield01').change(function() {
      var topic = jQuery('select#textfield01 option:selected').attr('title');
        // set the hidden input's value
        jQuery('#textfield1').val(topic);
        console.log(topic); 


<select name="textfield01" id="1015" class="{validate:{required:true}}">
    <option value="">Select</option>
    <option value="Local_Bus" title="220.00">Local_Bus</option>
    <option value="Link" title="360.00">Link</option>
</select>

Where my confusion is, is it possible to copy the value of hidden textfield03 in the same above function?

I figure if I can do that, I can add the condition and calculation as well.

Any help pointing me in the right direction is greatly appreciated.

PT

Upvotes: 0

Views: 54

Answers (2)

user1469
user1469

Reputation: 109

Thank you all for your help!

With your help I was able to come up with working code. Definitely feel free to let me know if it can be more efficient. I know it can and I will get there one of these days!

jQuery(document).ready(function() {
  // pass the title of the selected topic option to a hidden 'topic' form field
  jQuery('select#textfield01').change(function() {
      var original_cost_original = jQuery('select#textfield01 option:selected').attr('title');
      var original_cost = parseFloat(original_cost_original, 10);
      var discount_available_original = $("#textfield02").val();
      var discount_available = parseFloat(discount_available_original, 10);
        // set the hidden input's value
        jQuery('#textfield01').val(original_cost);
        if(discount_available > original_cost) {
            console.log("total cost+ is: " + original_cost/2);
            $("#textfield01").val(original_cost/2);
        }
        if(discount_available <= original_cost) {
            console.log("total cost- is: " + original_cost);
            $("#textfield01").val(original_cost);
        }
        console.log("original cost is: " + original_cost); 
        console.log("discount available is: " + discount_available); 
        console.log(x = discount_available - original_cost);
  })
})

Upvotes: 0

Barmar
Barmar

Reputation: 782130

Parse the values of the two text fields, do the comparison, and then set the value of the third text field with .val().

var val1 = parseFloat($("#textfield01").val());
var val2 = parseFloat($("#textfield02").val());
if (val2 > val1) {
    $("#textfield03").val(val2/2);
}

Upvotes: 1

Related Questions