Praful Bagai
Praful Bagai

Reputation: 17382

Get ids of checked boxes which are not disabled

I want to get the ids of all the check boxes which are checked and enabled, ie those checkboxes which are disabled and checked, should not be counted.

Here is my code:-

var widgets_list = [];
$("#dialog-form input:checkbox:checked").map(function(){
    widgets_list.push($(this).attr('id'));
});

The current code adds checkboxes which are disabled and checked, which I dont want. How do I add a functionality which will take into account only enabled checkboxes.

Upvotes: 0

Views: 82

Answers (4)

Joke_Sense10
Joke_Sense10

Reputation: 5402

It's pretty simple.Just add enabled.Something like this.

var widgets_list = [];
$("#dialog-form input:checkbox:checked:enabled").map(function(){
widgets_list.push($(this).attr('id'));
});

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You can also use :enabled,

$("#dialog-form input:checkbox:checked:enabled").map(function(){
    widgets_list.push($(this).attr('id'));
});

DEMO

Upvotes: 0

PSL
PSL

Reputation: 123739

Try:

var widgets_list = $("#dialog-form input:checkbox:checked").map(function(){
    if(!this.disabled)
       return this.id;
}).get();

or

var widgets_list = $("input:checkbox:checked:not(:disabled)").map(function(){
       return this.id;
}).get();

Demo

map is used to convert one collection to another collection in this case checkboxes to array of ids, so you can avoid a push which you would generally do with a loop (.each)

Upvotes: 4

Praveen
Praveen

Reputation: 56509

Try like this

$("#dialog-form input:checkbox:checked").not(":disabled").map(function(){
    widgets_list.push($(this).attr('id'));
});

JSFiddle

What you need to know is .not() and :disabled pseudo selector.

Upvotes: 2

Related Questions