0

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.

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

1 Comment

Thank you very mutch just what i needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.