Richard Peers
Richard Peers

Reputation: 1181

mootools 1.11 div contains checked checkbox

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

Upvotes: 2

Views: 2678

Answers (3)

user256911
user256911

Reputation: 1

for 1.1

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

Upvotes: -1

gonchuki
gonchuki

Reputation: 4134

"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();

Upvotes: 3

Sampson
Sampson

Reputation: 268364

From The Documentation:

$(sId).getElements("input:checked");

Upvotes: 0

Related Questions