Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove last Comma?

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> 
like image 499
Sanju Avatar asked Jan 12 '10 07:01

Sanju


1 Answers

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(', '); 
like image 74
Sam Doshi Avatar answered Sep 19 '22 04:09

Sam Doshi