vendredi 7 août 2015

Trouble trimming trailing comma from dynamically filled string

I am trying to trim the last character from a paragraph that I am dynamically filling with checkbox values. The trouble is that I must trim the trailing character of the whole paragraph. I do not want to trim it from the value before it is added to the list of values.

Here is a codepen:

http://ift.tt/1MfESjh

HTML:

<dl class="dropdown"> 

<dt>
<a href="#">
  <span class="hida">▼</span>     
</a>
</dt>

<dd>
    <div class="mutliSelect">
        <ul class="ul">
            <li>
                <input type="checkbox" value="PONumber" />Number</li>
            <li>
                <input type="checkbox" value="AuthNumber" />Auth Number</li>
            <li>
                <input type="checkbox" value="StatusId" />Status</li>
            <li>
                <input type="checkbox" value="ManufacturerId" />Manufacturer</li>
        </ul>
    </div>
</dd>
</dl>
<p class="multiSel"></p> 

Javascript:

$(".dropdown dt a").on('click', function () {
      $(".dropdown dd ul").slideToggle('fast');
  });

  $(".dropdown dd ul li a").on('click', function () {
      $(".dropdown dd ul").hide();
  });

  function getSelectedValue(id) {
       return $("#" + id).find("dt a span.value").html();
  }

  $(document).bind('click', function (e) {
      var $clicked = $(e.target);
      if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
  });


  $('.mutliSelect input[type="checkbox"]').on('click', function () {

      var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
          title = $(this).val() + ",";

      if ($(this).is(':checked')) {
          var html = '<span title="' + title + '">' + title + '</span>';
          $('.multiSel').append(html);
      } 
      else {
          $('span[title="' + title + '"]').remove();
          var ret = $(".hida");
          $('.dropdown dt a').append(ret);

      }
  });

There are a bunch of questions about trimming trailing characters on here, but I can't figure out how to trim the paragraph as a whole after a value is dynamically added. Thanks!




Aucun commentaire:

Enregistrer un commentaire