Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  31] [ 5]  / answers: 1 / hits: 65318  / 15 Years ago, tue, january 12, 2010, 12:00:00

This code generates a comma separated string to provide a list of ids to the query string of another page, but there is an extra comma at the end of the string. How can I remove or avoid that extra comma?



<script type=text/javascript>
$(document).ready(function() {
$('td.title_listing :checkbox').change(function() {
$('#cbSelectAll').attr('checked', false);
});
});
function CotactSelected() {
var n = $(td.title_listing input:checked);
alert(n.length);
var s = ;
n.each(function() {
s += $(this).val() + ,;
});
window.location = /D_ContactSeller.aspx?property= + s;
alert(s);
}
</script>

More From » jquery

 Answers
18

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(', ');

[#97858] Friday, January 8, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronniem

Total Points: 584
Total Questions: 111
Total Answers: 111

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
;