This question already has an answer here:
I am appending the values of selected checkboxes into a p tag. Right now, I have a comma that is added after each value. I am trying to change the code so there is not a comma after the value if it is either the only value selected or if it is the last value in the list. Any help would be appretiated.
Here is a codepen:
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);
}
});
Aucun commentaire:
Enregistrer un commentaire