Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery element list concat by separator

Tags:

jquery

I have a checkboxlist control and would like to get all the checkbox values that are checked concatenated into a string separated by '-'. Is there any functions for this? Ex:

$('#checkboxes input[type=checkbox]:checked').concat('-');
like image 290
user666423 Avatar asked May 04 '11 17:05

user666423


3 Answers

I think your best bet is

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

Basically, you're applying a function to each item that returns its value. Then you assemble the result into a string.

like image 167
Jacob Mattison Avatar answered Sep 21 '22 00:09

Jacob Mattison


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('-');
like image 33
Dutchie432 Avatar answered Sep 21 '22 00:09

Dutchie432


I think you want to look at jQuery's .map() functionality (not tested):

$('#checkboxes input[type=checkbox]:checked').map(function() {
  return $(this).attr('value');
}).get().join('-');
like image 23
Daff Avatar answered Sep 21 '22 00:09

Daff