Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create string from all checked values jQuery

Ok so I do this:

<input type="checkbox" class="checkBoxClass" value="0" />
<input type="checkbox" class="checkBoxClass" value="1" />
<input type="checkbox" class="checkBoxClass" value="2" />

Then in javascript/jquery I'm trying to do something like this:

$('#btnHtml').click(function(){
for( var checks=''; $('.checkBoxClass:checked').val() ){
    var checks = checks+', ';
}
});

Let's say all of those checkboxes are checked, how can I get var checks to be a string of '0, 1, 2' ?

Still trying to get used to the for() in JS, it might be the completely wrong way of going about it. Not sure how to do this.

like image 968
Silas Avatar asked May 16 '26 01:05

Silas


1 Answers

Functional programming:

var str = $('.checkBoxClass:checked').map(function() {
    return this.value;
}).get().join();

Reference: .map, .get, .join

like image 149
Felix Kling Avatar answered May 18 '26 13:05

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!