How can I add quotes to my jQuery array?

I'm pushing new values dynamically in my array using jquery.

which then generates an array that looks like this [1,2,3,4,5] and i want it to look something like this ['1','2','3','4','5']

so that it can be of use in my app.

EDIT

    $(document).ready(function(){

    $('.bookmark').live('click',function(){
    var element = $(this);
    var Id = element.attr("id");
    var I = element.attr("attr");
    var info = 'id=' + Id;
    var myArray = ('{{r.bookmarks}}');         #gets data from datastore..
    var myOtherArray = ''+Id;                  #current number i want to submit
    myArray.push(String( myOtherArray ));
    var arr = myArray;
    $('#array'+Id).val(arr);                   #newly formed array value added 
    var submitData = $('#array'+Id).serialize();
        $.ajax({
            type: "POST",
            url: "/addpage/{{user.email}}",  #page submit
            data: submitData,
            success: function(msg){

                if(parseInt(msg)!=0)
                {


                    $('.i'+Id).text('remove');}
            }

        });
    return false});
});

the array formed is 1,2,3,4,5,6,7,8,9,10 and i want it to be like this ['1','2','3','4','5','6','7','8','9','10'].

please help.

Upvotes: 0

Views: 4493

Answers (3)

Barmar
Barmar

Reputation: 780974

var array_of_strings = array_of_numbers.map(String);

Or do it when you're pushing:

arr.push(String(yournumber));

OK, now that you've posted your code. Change:

$('#array'+Id).val(arr);

to:

$('#array'+Id).val(JSON.stringify(arr.map(String)));

You can't put an array into the value of a form element. You can use JSON.stringify() to convert it to JSON format, which is Javascript literal array notation.

Upvotes: 6

Code Lღver
Code Lღver

Reputation: 15603

To make a number as string use the toString() function of Javascript.

Example:

var num = 15;
var n = num.toString();

Upvotes: 0

Lex Podgorny
Lex Podgorny

Reputation: 2930

[1,2,3,4,5].join(',').split(',');

Upvotes: 5

Related Questions