Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

jQuery: Remove comma from end of String

Hello All,

The following code works great in sharepoint 2010.  No Errors!!

It places Assistant's and Manager's from a list into individual table cells.  All the names are delimited by a comma (,).

Here's the problem I'm having.  How do I remove the last comma?
Examples of results:

John Hancock, John Doe, Jane Doe, Billy Dune,

<script type="text/javascript" src="/scripts/jquery-1.8.2.min.js" ></script>
<script type="text/javascript" src="/scripts/jquery.SPServices-0.7.2.min.js" ></script>
<style>
 .RoleHdr{background:#e1e1e1; font-weight:bold; padding:5px;}
</style>
<script>
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Joppy Business",
    CAMLViewFields: "<ViewFields><FieldRef Name='Name' /><FieldRef Name='Title' /></ViewFields>",
    CAMLQuery:"<Query>" +
                  "<OrderBy><FieldRef Name='Name' /></OrderBy>" +
                  //"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + Manager + "</Value></Eq></Where>"
          // "<Where><Or><Geq><FieldRef Name='Title' /><Value Type='Text'>" +
             //  "<Today /></Value></Geq><IsNull><FieldRef Name='Expires' />" +
             //  "</IsNull></Or>" +
             // "</Where>" +
           "</Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
      
      //Get Title
      var title = $(this).attr("ows_Title");   
      //alert("title:" + title);
      var name=$(this).attr("ows_Name").split(",");
      
      //check for Manager or Assistant
      if (title=="Assistant")
      {
        var PrHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";
       // alert(AsHtml);
      }
      else
      {
         var SrHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";      
      //   alert(liHtml);
      }

     
      
      //alert(name[0]);
      //alert(name[1]);
      //alert($(this).attr("ows_Name"));
       //var liHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";
        //alert(liHtml);
        $("#Assistant").append(PrHtml).css("padding","5px");
        $("#Mgr").append(SrHtml).css("padding", "5px");

      });
    }
  });
});
</script>
<table>
<tr>
 <td class="RoleHdr">Assistant</td>
 <td>
  <div id="Assistant"/>
 </td>
</tr>
<tr>
 <td class="RoleHdr">Manager</td>
 <td>
  <div id="Mgr"/>
 </td>
</tr>
</table>

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Here you go:

var myStr = "John Hancock, John Doe, Jane Doe, Billy Dune,";
myStr = myStr.slice(0, -1);

Open in new window

Avatar of Isaac

ASKER

how do I use that in the .each()?  Wouldn't it remove the (,) from each of the names?

completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
     
      //Get Title
      var title = $(this).attr("ows_Title");  
      //alert("title:" + title);
      var name=$(this).attr("ows_Name").split(",");
     
      //check for Manager or Assistant
      if (title=="Assistant")
      {
        var PrHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";
       // alert(AsHtml);
      }
      else
      {
         var SrHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";      
      //   alert(liHtml);
      }

     
     
      //alert(name[0]);
      //alert(name[1]);
      //alert($(this).attr("ows_Name"));
       //var liHtml = "<a href='" +name[0]+ "'>" + name[1] + "</a>, ";
        //alert(liHtml);
        $("#Assistant").append(PrHtml).css("padding","5px");
        $("#Mgr").append(SrHtml).css("padding", "5px");

      });
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ragural
ragural

In line 28.. put a check if name is blank. if yes then return or skip further block of code. This should work since your last split gives blank at the end.

 if (name=="") return;