user3080627
user3080627

Reputation: 5

jquery change array to string

using this little jquery i am able to save the data-id of the clicked list-element in an input-hidden-field:

(Important: Only one data-id can be saved in this input-hidden-field, if you click next element, this hidden-field gets updated.)

$(document).ready(function(){
    $('.ui-selectable :not([data-id=""])').click(function() {
        $('.ui-selectable .selected:not([data-id=""])').removeClass('selected');
        $(this).toggleClass('selected');
        $(this).parent().trigger('update');
    });

    $('.ui-selectable').on('update', function() {
        data = [];
        $(':not([data-id=""]).selected', this).each(function() {
            data.push( $(this).data('id') );
        });
    });
});

For now its saved in an array as you can see in:

data = [];

But i need to save it in a string, anybody could help with it?

Upvotes: 0

Views: 94

Answers (2)

stahlneckr
stahlneckr

Reputation: 149

The join method will give you a comma separated string of your array.

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

You can change that array into a string by using JSON.stringify,

Try this,

JSON.stringify(data);

Upvotes: 2

Related Questions