Hello people here is my code ...
$ (".input_check").change (function (){
if ($ (this).is (':checked')) {
console.log(this.id + 'is checked')
} else {
console.log(this.id + 'is not checked')
}
});
the above code works fine for several check boxes ....But i want to perform check after the page load not after $ (".input_check").change
.... I tried ...
$ (function () {
if ($ (".input_check").is (':checked')) {
console.log(this.id + 'is checked')
}
)}
Iam trying to find the ID this.id
but not wrking.. I also tried (this).id
and this.attr("id")
still not working... :( am getting undefined
anyways to fix this??
UPDATE :: I have multiple checkboxes which are checked... I am trying to find multiple ids of those check boxes which are checked
(this).id
and this.attr("id")
are not proper jQuery syntax for this situation. Should be
$(this).attr('id')
Fiddle
Edit based on your comments
Ok, then what stops you from doing the same on document ready?
$(function() {
$( ".input_check" ).each(function( index ) {
if ($ (this).is (':checked')) {
console.log($(this).attr('id') + 'is checked')
} else {
console.log($(this).attr('id') + 'is not checked')
}
});
});
Fiddle
To get the ID of all :checked
elements as an array:
var checked = $('.input_check:checked').map(function() {
return this.id;
}).get();
try:
$(this).attr('id')
and check if you have a ID attribute defined in the checkbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With