On Tue, Jan 18, 2011 at 9:47 AM, Diego Perini <[email protected]>wrote:
>
> $('#my-check-box').attr('checked', true);
>
> will confuse users into believe that "false" in the above statement is
> needed to achieve the opposite effect of unchecking the element
> attribute, which is not the case. Instead a "removeAttr()" will be
> needed to achieve that effect (with UI live attributes like
> "checked").
>
>
if you pass false to the checked attribute it will uncheck it.. (it probably
calls removeAttr internally)
> Thus the name ".attr()" is itself misplaced and the documentation
> misguiding people approaching Javascript and the DOM (read the
> comments there).
>
>
Yes, many jQuery methods have weird naming and doesn't match the standard
ones.. even things like "each()", "map()" doesn't behave like the native
ones.. (I always have to go back to the documentation because of things like
that) and yes, the comments usually have a bunch of bad practices.
> $('#my-check-box').is(':checked');
>
> this will not check the attribute value either, it will check the
> property value of the element (at least it should, not verified in
> jQuery).
>
> To check for an attribute value in jQuery one must use the attribute
> selector, like this (faster too):
>
> $('#my-check-box[checked]');
>
> this is as in CSS2.1 standard and would return correct results for the
> element "checked" attribute.
>
> The ":checked" pseudo-class will check the live DOM property value of
> the elements, not their attributes.
>
about the checked selector: http://api.jquery.com/checked-selector/ - it
is supposed to work just fine for checkboxes and radio buttons... probably
they convert it to an attribute selector internally...
jQuery is nothing about following the standards or best-practices and I
don't like that either.. but most of the times that I use jQuery is because
I'm working with someone that is used to it, so doing it on the "jQuery way"
makes more sense in this context.. future developers will understand what is
going on and if they don't there is documentation available about it..
I agree that jQuery is misleading in some aspects but I couldn't see a
context where the code that I provided wouldn't work.. I thought it was some
known bug related with this specific feature. But your points were valuable
anyways, thanks.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]