On Tue, Jan 18, 2011 at 7:15 PM, Miller Medeiros
<[email protected]> wrote:
>
> 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)
>
Yes it will uncheck it visually and set the property but will not
touch the HTML attribute.
It could be that it calls ".removeAttr()" internally. If it is that
way then it seems even that method fails.
>
>>
>> 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.
>
Attribute values can be be "string" or "null" (when not set). jQuery
changes this in a non cooperative way.
>
>>
>> $('#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...
>
I hope they don't do that but I haven't checked really.
> 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 am not forcing people to abandon their habits and knowledges, if
they understand the problems they will be able to fix them alone
(should it happen).
Keeping separate functionality for form submission and critical parts
and let jQuery handle animations/effects and a few Ajax calls seems a
balanced decision to avoid (or diminish) maintenance costs.
> 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.
>
It wasn't meant as a critic to you or the functionality of the
specific code you suggested.
My intervention was meant as a big warning against the (probably
unwanted) side effects produced by those specific methods and the
confusion they could create in future readers or in people trying to
learn HTML, DOM and Javascript.
Have fun,
--
Diego
> --
> 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]
>
--
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]