Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery this.id not working

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

like image 204
Friend Avatar asked Jun 19 '14 07:06

Friend


3 Answers

(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

like image 82
Hanky Panky Avatar answered Oct 13 '22 15:10

Hanky Panky


To get the ID of all :checked elements as an array:

var checked = $('.input_check:checked').map(function() {
    return this.id;
}).get();
like image 28
Alnitak Avatar answered Oct 13 '22 16:10

Alnitak


try:

$(this).attr('id')

and check if you have a ID attribute defined in the checkbox

like image 2
779IDEAS Avatar answered Oct 13 '22 15:10

779IDEAS