JQuery Checkbox and label

From CoderLearner
Jump to: navigation, search

Description

Get all checkbox checked elements and its associated label.

Example

<div id="cj_course">
<input type="checkbox" class="myibreaker" name="course[]" value="49" id="id49" checked="checked">
<label for="id49" title="FY14-Credit Card Data Security">SF1409</label>

<input type="checkbox" class="myibreaker" name="course[]" value="72" id="id72" checked="checked">
<label for="id72" title="FY14 P Card Cardholder">SF1402</label>

<input type="checkbox" class="myibreaker" name="course[]" value="71" id="id71" checked="checked">
<label for="id71" title="TAP Test">AW1400</label>

<input type="checkbox" class="myibreaker" name="course[]" value="51" id="id51" checked="checked">
<label for="id51" title="FY14-UHV Cash Handling">VF1401</label>

<input type="checkbox" class="myibreaker" name="course[]" value="52" id="id52" checked="checked">
<label for="id52" title="Asset Mngmnt: Business Manager">HF1408</label>

<input type="checkbox" class="myibreaker" name="course[]" value="53" id="id53" checked="checked">
<label for="id53" title="FY14 HR View">HH1401</label>

<input type="checkbox" class="myibreaker" name="course[]" value="54" id="id54" checked="checked">
<label for="id54" title="FY14 Voucher Creation">HF1401</label>

</div>

Get all the checkbox checked

 var tmp_checkbox1 = $("#cj_course input:checked").map(function() {
            return this.value;
        }).get().join(',');

Get all the checkbox checked label


        var course_aa = [];
        
        $.each($('input[name="course[]"]:checked'), function(k) {
            course_aa.push($(this.nextSibling).text());
        });
        console.log(course_aa.join(','));