javascript - Trouble trimming trailing comma from dynamically filled string -


i trying trim last character paragraph dynamically filling checkbox values. trouble must trim trailing character of whole paragraph. not want trim value before added list of values.

here codepen:

http://codepen.io/cavanflynn/pen/pqlwkj

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 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 bunch of questions trimming trailing characters on here, can't figure out how trim paragraph whole after value dynamically added. thanks!

the following code want. moved comma outside <span>

$('p.multisel')     .html($(this)         .find('input[type="checkbox"]:checked')         .map(function(idx, elem) {             return $(elem).parent().text().trim();         })         .get()         .map(function(text) {             return "<span title='" + text + "'>" + text + "</span>";         })         .join(', ')); 

codepen

explanation

$('p.multisel').html(...) 

(.html) sets contents of <p class='multisel'>.

$(this).find('input[type="checkbox"]:checked') 

(.find) gets checked check boxes.

.map(...) 

(.map) maps array of checked check boxes plain text, whitespace trimmed.

.get() 

(.get) turns jquery selection regular array.

.map(...) 

(array.prototype.map) maps plain text element wanted make wrapping in <span> tags , stuff.

.join(...) 

(array.prototype.join) inserts commas , spaces between elements of array working on.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

php - How do you embed a video into a custom theme on WordPress? -