Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery join multiple labels

Tags:

jquery

join

Hi I have a div element that contains a list of checkboxes like this

 <ul>
 <li><input type="checkbox" ID="check1" /><label for="check1">Monday</label></li>
 <li><input type="checkbox" ID="check2" checked="checked"/><label for="check2">Tuesday</label></li>
 <li><input type="checkbox" ID="check3" /><label for="check3">Wednesday</label></li>
 <li><input type="checkbox" ID="check4" checked="checked"/><label for="check4">Thursday</label></li>
</ul>

I want to get the values checked in a string like 'Tuesday-Thursday'. So I wrote this very concise jQuery instruction

$('input:checked', 'ul').next().text() 

This give me 'TuesdayThursday'. I could not find the way to pass a separator and have 'Tuesday-Thursday'

like image 872
user385411 Avatar asked Jan 21 '23 01:01

user385411


1 Answers

Try something like

$('input:checked', 'ul').map(function(){
    return $(this).next().text();
}).get().join('-');
like image 133
rahul Avatar answered Jan 31 '23 08:01

rahul