jQuery .each: Do not add comma in last field

javascript, jquery

Solution

I always use arrays for these kind of things:

var fields = $('.wrap').find(".text[value!='']");
var data = [];

fields.each(function() {
    data.push($(this).val());
});

alert(data.join(','));

Problem

I want to get values of all fields in a variable separated by a comma. For example: `1,2,3` The following code will work fine, but it only adds the comma at end of the last value also. How can I remove that? ``` fields = $('.wrap').find('.text'); var data = ''; fields.each(function() { var value = $(this).val(); if ( value != '' ) { data += ' ' + value + ','; } }); alert(data); ``` JSFiddle: http://jsfiddle.net/cn5Gt/

Original source