I've got checkboxes with labels like 1 some text I would like to get the number out of the labels of the checkboxes that are checked and add them together. I was trying to do something like what I have below but it is not giving me anything.
var total = 0;
$('input:checked').each(function () {
total += parseInt($("label[for='" + this.id + "']").text());
});
Here is an example checkbox
<label><input type="checkbox" id="enbapicks-1" name="ENBApicks[1]" value="1" checked> 1 Golden State</label>
<label><input type="checkbox" id="enbapicks-5" name="ENBApicks[5]" value="1" checked> 5 Memphis</label>
Your
labeldoesn't haveforattribute, that's why$("label[for='" + this.id + "']")returns blank, and thus.text()returns empty string.You should specify it like this:
If you can't change HTML, you could get the
labelfrominputwithparent()method:Alternatively you could set the number as
input'svalueattribute, then useval()method to grab it frominputelements.