views:

603

answers:

3

how can mootools 1.11 determine if a div contains any checked check boxes?

tried all kinds of variations using $ $$ $E $ES getElements and css selectors, its just not returning true if this div contains no tick boxes

var ticked = $(sId).getElements('[checked=checked]');
if($chk(ticked)){alert('yo');}else{unticked = true;}
A: 

From The Documentation:

$(sId).getElements("input:checked");
Jonathan Sampson
doesnt work for 1.11 :(
A: 

for 1.1 console.log( $('id').getProperty('checked') );

+2  A: 

"checked" is a dynamically assigned DOM property (which is a boolean), and accessing the attribute only returns the value that was there when the page loaded (or the value you placed when using setAttribute).
Also, as MooTools 1.11 does not have complex selectors (attributes can not be filtered directly within $$) and the filterByAttribute function only accepts direct string comparison, this is your only (and best!) option:

$(sId).getElements('input').filter(function(input) {
    return /radio|checkbox/.test(input.getAttribute('type')) && input.checked;
})

note: I added radio just for completeness, the filter loop would have to be run anyways to verify the checked status.

If you want to simplify the process and be able to reuse the code (the MooTools way), you can also mix-in the checked filter into Elements like this:

Element.extend({
    getCheckedInputs: function() {
        return this.getElements('input').filter(function(input) {
            return /radio|checkbox/.test(input.getAttribute('type')) && input.checked;
        });
    }
});

and then your selection call is reduced to:

$(sID).getCheckedInputs();
gonchuki