I am currently working on displaying the selected filters from checkboxes. Initially, I create an array containing the values selected from the checkboxes and then aim to add them to a string.
Suppose I have two checkboxes labeled: label1
and label2
, my array looks like this: ['label1','label2']
.
The expected outcome should be "Filtered by: Label1, Label2
" but instead, I end up with
Filtered by: Label1,Label2, Label1,Label2
I believe the issue lies within the for loop where I construct the string, as the array appears correct
let topLabel = jQuery('.industry-filter').data('selected-text') + ' ';
let termsName= ['Industry', 'Category']
if(termsName.length){
for(var j = 0; j < termsName.length; j++){
if(j == termsName.length - 1){
topLabel += termsName;
}else{
topLabel += termsName+', ';
}
}
}else{
topLabel = jQuery('.industry-filter').data('text');
}
$('.industry-filter').text(topLabel);
If you'd like to see the problem in action, check out this pen: https://codepen.io/anon/pen/pqjYxL