0

I have written some jQuery code to dynamically update a hidden input value based on the quantity that the user chooses:

$('input[name^="quant"]').change(function () {
       updateTotal();
    });

function updateTotal() {
        var count = 1;
        $('input[name^="quant"]').each(function() {
            var qty = $(this).val();
            var price = $(this).siblings('input[name^="price"]').val();
            $('input[name=item_name_'  + count + ']').val(qty + ' x ' + $('input[name=item_name_'  + count + ']').val());

            count++;
        });
    }

The line $('input[name=item_name_' + count + ']').val(qty + ' x ' + $('input[name=item_name_' + count + ']').val()); replaces the entire input by appending the value of the current input value on the end.

For example if the quantity is 4 then the element will look like this:

<input type="hidden" name="item_name_1" value="4 x Front Row Seats">

The problem I am having is as this is happening on change, it will do this every time i change the quantity:

<input type="hidden" name="item_name_1" value="5 x 4 x Front Row Seats">
<input type="hidden" name="item_name_1" value="3 x 5 x 4 x Front Row Seats">
<input type="hidden" name="item_name_1" value="4 x 3 x 5 x 4 x Front Row Seats">

and so on...

Is there a way I can save the value on the hidden input before I do this so it never appends like this?

user3574492
  • 6,225
  • 9
  • 52
  • 105
  • It would make ***far*** more sense to just have the quantity in the hidden field. You don't need the additional seat-type text at all – Rory McCrossan May 15 '17 at 10:14
  • @RoryMcCrossan The input field needs to show the quantity followed by the item name as it's a PayPal hidden input that is being generated. – user3574492 May 15 '17 at 10:16

2 Answers2

0

I found a better way around this. As I was trying to set the quantity for a PayPal item I used this approach.

I just needed to set the quantity as a hidden input for the item rather than manually trying to create the value to show it as @Rory McCrossan pointed out:

<input type="hidden" name="quantity_1" value="1">
Community
  • 1
  • 1
user3574492
  • 6,225
  • 9
  • 52
  • 105
0

Check this one

$('input[name^="quant"]').change(function() {
  updateTotal();
});

function updateTotal() {
  var count = 1;
  $('input[name^="quant"]').each(function() {
    //alert($(this).val());
    var qty = $(this).val();

    $('input[name=item_name_' + count + ']').val(qty + ' x '+'Front Row Seats');

    count++;
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <label>Quantity</label>  <input type="text" name="quant"><br />

<label>Result</label>    <input type="text" name="item_name_1" value="">
User1793
  • 47
  • 2
  • You've just hard coded `Front Row Seats` into the value. This is wrong, the name needs to be fetched from whatever the current input value is. – user3574492 May 15 '17 at 10:48
  • Are you saying that name i.e., "Front Row Seats" is present in separate textbox as a value? – User1793 May 15 '17 at 11:14
  • No, I'm saying that `Front Row Seats` is the text that is coming from another hidden input as you can see in my code: `$('input[name=item_name_' + count + ']').val(qty + ' x ' + $('input[name=item_name_' + count + ']').val());` – user3574492 May 15 '17 at 11:16