views:

38

answers:

2

I need a function that accepts a parameter with its id example a div and after that loops inside the div to look for checkboxes and if there is/are any checks if its value is checked and returns true if every checkbox is checked returns false.

+1  A: 

You can do something like

$('#parent:has(input:checkbox:checked)')

If you find any elements you can retrun true, for example by .is('*') or .length == 1

You can write a function, or even a tiny plugin:

$.fn.extend({   
     hasCheckedBoxes: function() {  
       return this.has('input:checkbox:checked').is('*');
     }  
});

This can be used as:

if($('#Documents').hasCheckedBoxes()) //`#Documents` contains a checked box.

or

if($('div').hasCheckedBoxes()) //true if any `<p>` contains a checked box.
Kobi
A: 

Hopefully I've understood you correctly. This is what I think you've asked for: a function that takes an element ID and returns false if the corresponding element contains any checked checkboxes and true otherwise.

Here's a no-library version. It will be faster and work in more browsers than a jQuery version.

function containsNoCheckedCheckboxes(id) {
    var el = document.getElementById(id);
    if (el) {
        var inputs = el.getElementsByTagName("input");
        for (var i = 0, len = inputs.length; i < len; ++i) {
            if (inputs[i].type == "checkbox" && inputs[i].checked) {
                return false;
            }
        }
    }
    return true;
}
Tim Down
Thank you very mutch just what i needed.
Adem