Getting value of update input with jquery

Im developing Html Form where value will update using jquery as user input the value.


When you enter value in Enter Your Value field it will instantly update its price in Price field

Now i want to add all field sum which will be field 1,field 2,field 3,field 4,field 5 and field 6 and display the total sum in Total Amount but total calculation of the all field are not updating in Total Amount. As price field only get update after we enter value in Enter Your Value Field.
Html Code

<form action="" method="post">
      <div class="row">
        <div class="col-md-6">
          <div class="mb-3">
            <input type="text" class="form-control" name="fullname" placeholder="FUll Name">
          </div>
        </div>
        <div class="col-md-6">
          <div class="mb-3">
            <input type="email" class="form-control" name="email" placeholder="Your Email">
          </div>
        </div>
        <div class="col-md-6">
          <div class="mb-3 menier">
            <h4>Iron Bark:</h4>
            $210 Per Cubic Metre 
            <input type="number" id="ironbark" class="form-control" placeholder="Enter Your Value">
            <div class="mt-3">
                Price 
            <input id="ironbarvalue" type="text" name="ironbark" class="total-amount form-control"  readonly>
            </div>
            
          </div>
        </div>
        <div class="col-md-6 ">
          <div class="mb-3 menier">
            <h4>Red Gum:</h4>
            $200 Per Cubic Metre 
            <input type="number" id="redgum" class="form-control" placeholder="Enter Your Value">
            <div class="mt-3">
              <span class="sammy">Price </span>
            
            <input type="text" id="redgumvalue" name="redgum" class="total-amount form-control"  readonly>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="mb-3 menier">
            <h4>Mixed Gum:</h4>
            $100 Per Cubic Metre 
            <input type="number" id="mixedgum"  class="form-control" placeholder="Enter Your Value">
            <div class="mt-3">
            Price <input id="mixedgumvalue" type="text" name="mixedgum" class="total-amount form-control"  readonly>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="mb-3 menier">
            <h4>Brown Bark:</h4>
            $110 Per Cubic Metre <input id="brownbark" type="number" class="form-control" placeholder="Enter Your Value">
            <div class="mt-3">
            Price <input id="brownbarkvalue" type="text" name="brownbark" class="total-amount form-control"   readonly>
            </div>
          </div>
        </div>
        <div class="col-md-6 mt-2">
          <div class="mb-3">
            <input type="checkbox" class="form-check-input" id="stackfee" name="stackingfee" value="25">
            <label class="form-check-label" for="exampleCheck1">Stacking Fee: $25 Per m3 </label>
          </div>
        </div>
        <div class="col-md-6 mt-2">
          <div class="mb-3">
            <input type="checkbox" class="form-check-input" id="delivery" name="deliveryfee" value="50">
            <label class="form-check-label" for="exampleCheck1">Delivery Fee: $50 Minimum  </label>
          </div>
        </div>
        <div class="col-md-12 mb-3">
          <strong class="">
          Total Amount: $
          </strong>
          <input type="number" class="form-control total-amount" id="sum" name="totalsum" disabled>
        </div>
        <div class="col-md-12">
          <button type="submit" name="submit" class="btn btn-primary">Submit</button>
        </div>
      </div>
    </form>

Jquery code

$(document).ready(function(){

  $('#ironbark').change(function(){
    var ironbarkrate = parseFloat($('#ironbark').val()) || 0;
    $('#ironbarvalue').attr('value', 210 * ironbarkrate);    
  });

  $('#redgum').change(function(){
    var redgum = parseFloat($('#redgum').val()) || 0;
    $('#redgumvalue').attr('value', 200 * redgum);    
  });

  $('#mixedgum').change(function(){
    var mixedgum = parseFloat($('#mixedgum').val()) || 0;
    $('#mixedgumvalue').attr('value', 100 * mixedgum);    
  });

  $('#brownbark').change(function(){
    var brownbark = parseFloat($('#brownbark').val()) || 0;
    $('#brownbarkvalue').attr('value', 110 * brownbark);   

  });

  $('#ironbarvalue, #redgumvalue,#mixedgumvalue,#brownbarkvalue').change(function(){
        var value1 = parseFloat($('#ironbarvalue').val()) || 0;
        var value2 = parseFloat($('#redgumvalue').val()) || 0;
        var value3 = parseFloat($('#mixedgumvalue').val()) || 0;
        var value4 = parseFloat($('#brownbarkvalue').val()) || 0;
        var value5 = parseFloat($('#stackfee').val()) || 0;
        var value6 = parseFloat($('#delivery').val()) || 0;
        $('#sum').attr('value',value1 + value2 + value3 + value4 + value5 + value5);
   });

});

Im Trying to get All sum on Total Amount… but im notable to get Sum display in Total Amount, isnt because Price field is generate by Jquery

Why don’t you set the total account directly when a single amount is changed?

$('#ironbark').change(function(){
    var ironbarkrate = parseFloat($('#ironbark').val()) || 0;
    $('#ironbarvalue').attr('value', 210 * ironbarkrate);    
   updateSum();
  });

  function updateSum()
  {
    $(’#sum’).val(parseFloat($('#ironbarvalue').val()) + ……);
  }

You do not need to use attr(‘value’ …. As you can directly set the value with the val() function.

P.S.

Can someone explain to me why

parseFloat($(‘#mixedgum’).val()) || 0

Works? In my opinion result of the || must be a Boolean and not a float value?

Its not working… Total Amount doesnt change on entering value

parseFloat

incase if someone enter letters to avoid its and excepting number only

Im accepting result like below, when i quantity on iron bark total price 1x210= 210 and mixed gum total price 2x100 = 200 and Total Amount: iron-bark(210) + mixed gum(200). So Total Amount= 410

Your last function is waiting for a change event on these items.

  $("#ironbarvalue, #redgumvalue,#mixedgumvalue,#brownbarkvalue").change(
    function () {

However those elements are being changed dynamically so there is no change event associated with them. You would need to trigger the change yourself with .trigger("change");

  $("#ironbark").change(function () {
    var ironbarkrate = parseFloat($("#ironbark").val()) || 0;
    $("#ironbarvalue")
      .attr("value", 210 * ironbarkrate)
      .trigger("change");
  });

  $("#redgum").change(function () {
    var redgum = parseFloat($("#redgum").val()) || 0;
    $("#redgumvalue")
      .attr("value", 200 * redgum)
      .trigger("change");
  });

  $("#mixedgum").change(function () {
    var mixedgum = parseFloat($("#mixedgum").val()) || 0;
    $("#mixedgumvalue")
      .attr("value", 100 * mixedgum)
      .trigger("change");
  });

  $("#brownbark").change(function () {
    var brownbark = parseFloat($("#brownbark").val()) || 0;
    $("#brownbarkvalue")
      .attr("value", 110 * brownbark)
      .trigger("change");
  });

(Note that you add the $50 dollar delivery fee automatically but you don’t show the checkbox as checked. It looks odd).

As @Thallius said it seems a long winded way of doing things.

1 Like

Having a bit of a play with this as well.

You can use event delegation in jquery, so here I am sticking an eventListener on the form element. I have given the form element an id of ‘wood-order-form’. The quantity inputs I have given a className of ‘wood-qty’

  $('#wood-order-form')
    .on(
      'keyup change', // listen to keyup and change events
      '.wood-qty',  // filtered to inputs with a className of wood-qty
      (event) => { 
        updatePrice(event.target)
        updateTotal()
      }
    )

I have also used a lookup using the input’s id name to get the individual wood prices

const perCubicMetre = {
  ironbark: 210,
  redgum: 200,
  mixedgum: 100,
  brownbark: 110
}

// example
const name = input.id // e.g. 'redgum'
perCubicMetre[name] // 200

Here is the code. It needs work though. In its current state the stacking fee is added regardless e.g. not optional. The delivery cost also needs to be incorporated.

$(document).ready(function() {
  
  const stackingFee = 25
  
  const perCubicMetre = {
    ironbark: 210,
    redgum: 200,
    mixedgum: 100,
    brownbark: 110
  }
  
  const updatePrice = (input) => {
    const treeName = input.id
    const qty = parseFloat(input.value) || 0
    const rate = perCubicMetre[treeName]
    
    // #${treeName}value e.g. #ironbarkvalue
    $(`#${treeName}value`).attr('value', rate * qty)
  }
  
  // general function to total all the values of input elements
  const sumInputValues = (inputs) => {
    return inputs.reduce(
      (total, input) => { 
        return total + (parseFloat(input.value) || 0) 
      }, 0
    )
  }
  
  // total quantities * 25
  const getStackingFee = () => {
    const qtyInputs = $('.wood-qty').get() // need an array
    return stackingFee * sumInputValues(qtyInputs)
  }
  
  const updateTotal = () => {
    const totalInputs = $('.wood-total').get()
    const total = sumInputValues(totalInputs) + getStackingFee()
    
    $('#sum').attr('value', total)
  }
  
  $('#wood-order-form')
    .on(
      'keyup change', 
      '.wood-qty', 
      (event) => { 
        updatePrice(event.target)
        updateTotal()
      }
    )
});

2 Likes

I don’t know why, but I decided to fool around with this and see if I could do it with Vanilla JavaScript.

https://codepen.io/Strider64/pen/MWBRxrV

2 Likes

there is option to pick up or delivery, if its delivery then $50 will charge but if client comes to pickup then there is no delivery charge… delivery will added only if its checked…

2 Likes

working thank you for your time

2 Likes

working as i wanted thank you for your time

1 Like

thank for the solution got , next way to solve as im not much into javascript

im trying to add value only when checkbox is checked for stacking and delivery fee.

$('#brownbark').change(function(){
        var brownbark = parseFloat($('#brownbark').val()) || 0;
        $('#brownbarkvalue').attr('value', 110 * brownbark).trigger("change");    

      });

      //Stacking Fee
      $('#stackfee').change(function(){
        if ($(this).is(':checked')) {
            $(this).next('input[type="text"]').val(25).trigger("change");
          } else {
            $(this).next('input[type="text"]').val(0).trigger("change");
        } 

      });

      //Delivery Fee
      $('#delivery').change(function(){
        if ($(this).is(':checked')) {
            $(this).next('input[type="text"]').val(50).trigger("change");
        } else {
            $(this).next('input[type="text"]').val(0).trigger("change");
        } 
      });

      $('#ironbarvalue, #redgumvalue,#mixedgumvalue,#brownbarkvalue,#stackfee,#delivery').change(function(){
            var value1 = parseFloat($('#ironbarvalue').val()) || 0;
            var value2 = parseFloat($('#redgumvalue').val()) || 0;
            var value3 = parseFloat($('#mixedgumvalue').val()) || 0;
            var value4 = parseFloat($('#brownbarkvalue').val()) || 0;
            var value5 = parseFloat($('#stackfee').val()) || 0;
            var value6 = parseFloat($('#delivery').val()) || 0;
            $('#sum').attr('value',value1 + value2 + value3 + value4 + value5 + value6 );
       });

but code is not working , even when checkbox is unchecked its adding value. I want to add value in Total Amount only when Stacking Fee & Delivery Fee are checked.

is the value adding because i have predefine value on checkbox…

<div class="mb-3">
  <input type="checkbox" class="form-check-input" id="stackfee" name="stackingfee" value="25">
  <label class="form-check-label" for="exampleCheck1">Stacking Fee: $25 Per m3 </label> </div>
<div class="mb-3">
  <input type="checkbox" class="form-check-input" id="delivery" name="deliveryfee" value="50">
   <label class="form-check-label" for="exampleCheck1">Delivery Fee: $50 Minimum  </label>
</div>

11
but Total Amount value is blank, there nothing…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.