I’m trying to retrieve the text for selected check boxes like so:
HTML:
<label class="checkbox">
<input type="checkbox" name="priority" value="2" checked="checked">2 - Critical
</label>
<label class="checkbox">
<input type="checkbox" name="priority" value="3">3 - Important
</label>
jquery:
$('#priorityContents input:checkbox:checked').each(function() {
if(priorityText.length > 0) {
priorityText = priorityText + "|";
}
priorityText = priorityText + $(this).text();
});
alert(priorityText);
I would expect to see:
2 - Critical
I don’t get any errors in my console. What am I missing?
You want to get to the
labelelement, which is the parent of theinput:Here’s the fiddle: http://jsfiddle.net/Hk63N/
For increased performance, you should consider splitting up the selector:
From the jQuery docs:
Here’s the fiddle for this: http://jsfiddle.net/Hk63N/1/