How to Remove last Comma?
Use Array.join
var s = "";
n.each(function() {
s += $(this).val() + ",";
});
becomes:
var a = [];
n.each(function() {
a.push($(this).val());
});
var s = a.join(', ');
s = s.substring(0, s.length - 1);
You can use the String.prototype.slice
method with a negative endSlice
argument:
n = n.slice(0, -1); // last char removed, "abc".slice(0, -1) == "ab"
Or you can use the $.map
method to build your comma separated string:
var s = n.map(function(){
return $(this).val();
}).get().join();
alert(s);