JQuery selector for flagged checkbox

We can select the checkbox (in jQuery) as follows:

jQuery(:checked) 

But how can we select the label of the checked checkbox in jQuery?

+4
source share
3 answers

the does not checkbox comes with text (in asp.net it is )

 jQuery(":checked").next('span').html() //next or prev ( depends n direction) 
+4
source

label checkbox element. Just find it using the for attributes:

In html, it looks like this:

 <input type="checkbox" name="chk" /> <label for="chk">Checkbox 1</label> 

You can find it as:

 jQuery('input[type=checkbox]:checked').map(function() { return $('label[for=' + $(this).attr('name') + ']'); }); 

This will return all flag labels. The label should not be next to the previous item next to the check box.

+2
source

Assuming you have this html

 <label>Label</label> <input type="checkbox" checked="checked"> test </input> <label>Label 2</label> <input type="checkbox" > test 2 </input> <label>Label 3</label> <input type="checkbox" > test 3</input> 

you can find the shortcut using

 $("input:checked").prev('label'); 

* use .next if you have a label after the checkbox in your html

* use .parent if you wrap the check box in the label element

+2
source

All Articles