Tony
Tony

Reputation: 12695

How to check by jQuery the value some checked checkboxes attribute value?

I need some help, I have some checkboxes like that

<input class="some-class" type="checkbox" has-format="0">

or

<input class="some-class" type="checkbox" has-format="1">

Is it possible, to check, if all of that checked checkboxes has the has-format value = 1 or 0 ? I mean, e.g. if I want to check = 1 then if any of the checked checkboxes has that value 0 then I'll get false

Upvotes: 0

Views: 61

Answers (2)

Rob W
Rob W

Reputation: 349042

You can combine the :checked selector with the attribute selector. The following variable is only true if all of the specified conditions are met.

var checked_and_format1 = !$('.some-class:checked[has-format!=1]').length;
// Selects:
//  .some-class 
//  which is checked
// and has a `has-format` property which is not 1.
// If the collection's size is zero, then we can assume that the whole
//  .some-class:checked collection is valid.

If you're sure that there's no other value besides 1 and 0, you can replace [has-format!=1] with [has-format=0].

Upvotes: 2

gdoron
gdoron

Reputation: 150263

var isValid = $('.some-class:checked[has-format="0"]').length == 0;

Upvotes: 1

Related Questions