Daniel_12
Daniel_12

Reputation: 103

Append value to input field using jQuery

I have a small form with three fields and I can submit that form choose different options next time. I am submitting this form with ajax call.

Now on form submit I want to post all the selected options. For this, I have created a hidden field like

<input type="hidden" name="selectedproduct[]" id="sel-product">

and ajax success method am appending value like

 $('#sel-product').val(element.product);

But here I am getting only one value instead of an array. How can I append all the values to the field?

Upvotes: 0

Views: 5448

Answers (1)

Ignacio Ramos
Ignacio Ramos

Reputation: 61

Assuming the product fields with class ".element_product".

//init the array products
arrp=[];


//loop the elements
    $('.element_product').each( function () 
      {
            arrp.push( $(this).val() );
      });

//Pass the array to field
$('#sel-product').val(arrp);

Upvotes: 2

Related Questions