1

I have a following variable

var $pk3s_c = $('<input id = query_form_tbl_info_'+query_index +'_pk3ss[] name =query_form[tbl_info]['+query_index+'][pk3ss][] type = hidden></input>');

and an array

var pk3s = opts[tbl]["cols"];

during iteration through the array, I want to append the elements of an array to $pk3s_c

  $.each(pk3s, function(i,pk3){
  $pk3s_c.attr('value',pk3);
  })

the code above is not working, it shows me that I have appended only last element of a pk3s, and not all of them. How can I append each element of p3ks into my hidden input?

Alina
  • 2,191
  • 3
  • 33
  • 68
  • 1
    Maybe: `$pk3s_c.attr('value', $pk3s_c.attr('value') + pk3);` or you might want to have a `separator` between the values than: `$pk3s_c.attr('value', $pk3s_c.attr('value') + ':' + pk3);` – Ofir Baruch Jun 25 '15 at 11:16
  • it seems it produces an array with one element and this element is a collection of all pk3s. so if pk3s =["banana","orange"], your line will produce [":banana:orange"]. I need it as an array of elements of pk3s – Alina Jun 25 '15 at 11:25
  • possible duplicate of [How i can store javascript array object saved somehow so that I can use it later](http://stackoverflow.com/questions/7086208/how-i-can-store-javascript-array-object-saved-somehow-so-that-i-can-use-it-later) – John Jun 25 '15 at 11:39

2 Answers2

0

For readability, I would pass the arguments as a parameter to the jQuery function

Would look like

var values = ['argument1', 'argument2', 'argument3'];
var query_index = 43;

var jqueryAttributes = {
 id: 'query_form_tbl_info_' + query_index + '_pk3ss[]',
 name: 'query_form[tbl_info][' + query_index + '][pk3ss][]',
 type: 'hidden',
 value: JSON.stringify(values)
  // if your server don't support HTML encoding, use the one below instead
  // value: "['"+values.join("','")+"']"
};

var $newElement = $('<input/>', jqueryAttributes);

alert('you element would look like : ' + $newElement[0].outerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
zeachco
  • 754
  • 4
  • 16
  • Your values look like "argument1argument2argument3". My container is a vector because of pk3ss[] and I want to save my arguments as a vector there, so that the values look like ["argument1","argument2","argument3"] – Alina Jun 25 '15 at 11:36
  • ok wasn't specified how it should look, JSON.stringify instead of .join would transform your array into a string that look like ['argument1', 'argument2', ...] I've edited my answer – zeachco Jun 25 '15 at 11:40
0

You aren't going to get an array into a string field without converting it into a string.

The JSON format is very useful for this

// convert the array to a string
var myString = JSON.stringify(myArray);

// put the string into the field as it's value
$('input').val(myString);

Javascript can interpret the resulting string and server side languages can easily convert it from a string into an array value they can understand (see for php, ruby)

Here is an example in a jsfiddle

Community
  • 1
  • 1
Toni Leigh
  • 4,830
  • 3
  • 22
  • 36