List of jquery concat delimiter items

I have a checkboxlist element and I would like all the checkboxes that were checked to be combined into a line separated by '-'. Are there any features for this? Example:

$('#checkboxes input[type=checkbox]:checked').concat('-'); 
+6
source share
5 answers

I think your best bet is

 $('#checkboxes input[type=checkbox]:checked').map(function() { return $(this).val(); }).get().join('-'); 

Basically, you apply a function to every element that returns its value. Then you collect the result in a string.

+11
source

Check out this previous post . Perhaps using the map() function will work for you.

 $('#checkboxes input[type=checkbox]:checked').map(function() { return $(this).val(); }).get().join('-'); 
+5
source

I think you want to see jQuery . map () (not tested):

 $('#checkboxes input[type=checkbox]:checked').map(function() { return $(this).attr('value'); }).get().join('-'); 
+3
source

It depends on what you are trying to do. For instance -

 var list = '' $('#checkboxes input[type=checkbox]:checked').each(function(){ list += $(this).val() + '-' }); 

It will give you a list with delimiters separated by a dotted line, but if you want to do this to process / submit the form, check .serialize()

0
source

I donโ€™t think you could do it in one step. Here is a similar question, check the answer: How to get checkbox values โ€‹โ€‹in jQuery

-1
source

Source: https://habr.com/ru/post/887299/


All Articles