Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't [checked] work as selector in JQUERY?

Tags:

jquery

It seems that the only way to select a checkable input that is actually checked is to use the :checked selector.

Is there a technical reason why this does not work:

$("INPUT[checked]")

whereas it seems to work for other attributes?

like image 526
Tom Hubbard Avatar asked Feb 21 '26 13:02

Tom Hubbard


1 Answers

Because you would need to put the value for an attribute. But what is it? You write in XHTML checked="checked", in HTML4/5 just plain checked. The DOM model should store it as true.

$('input[checked="checked"]'); // Poor
$('input[checked=true]'); // Maybe

But it is fiddly because of potential browser differences, and checking a boolean, so there is a specific way to do it.

$('input:checked'); // Correct

The internal implementation just checks the elements .checked attribute.

From v1.5.1:

filters: {

    // ...

    checked: function( elem ) {
        return elem.checked === true;
    },

    // ...
}
like image 72
Orbling Avatar answered Feb 24 '26 05:02

Orbling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!