Reputation: 64
I have this checkbox:
<input type="checkbox" name="foo[]" value="111111">
<input type="checkbox" name="foo[]" value="222222">
<input type="checkbox" name="foo[]" value="333333">
And i'm trying to get the value from the selected checkbox
$("input:checkbox[name^='foo']").each(function(){
var val = $(this).val();
alert(val);
});
But the event isn't called.. Am I doing something wrong?
Sorry for bad english!
Upvotes: 0
Views: 1974
Reputation: 24638
Something very fundamental many of us for get is DOM ready:
$(function() {
$(":checkbox[name^='foo']").on('change', function () {
alert(this.value + ' --- ' + this.checked);
});
});
Upvotes: 1
Reputation: 25537
USe is(":checked")
to check whether the checkbox is checked
$("input:checkbox[name^='foo']").click(function () {
$("input:checkbox[name^='foo']").each(function () {
if ($(this).is(":checked")) {
alert($(this).val());
}
});
});
Upvotes: 1
Reputation: 15112
Don't use each
.
Use .on('change')
for your event. Also this.value
is easier than $(this).val()
.
$("input:checkbox[name^='foo']").on('change', function () {
var val = this.value;
alert(val);
});
Upvotes: 1