On Jan 18, 7:51 am, Miller Medeiros <[email protected]> wrote:
> On Mon, Jan 17, 2011 at 10:54 PM, Diego Perini <[email protected]>wrote:
>
>
>
> > On Mon, Jan 17, 2011 at 11:52 PM, Miller Medeiros
> > <[email protected]> wrote:
>
> > > $('#my-check-box').attr('checked', true); -> should work cross browser
> > if
> > > it is a checkbox.
>
> > > and you can check if a checkbox is checked by using
> > > `$('#my-check-box').is(':checked')`...
>
> > these two $() statements alone deserve a long chapter by themselves to
> > exactly explain all the inconsistencies that may arise by using them
> > together (probably even for checkboxes). There is also a problem with
> > mixing strings and booleans to consider in your example (or in jQuery
> > anyway).
>
> > Comparing values obtained by direct DOM properties access
> > (pseudo-selector) with values obtained by accessing HTML attributes
> > through getter/setter in that way is scary at best (maybe worth a
> > digest in JSMentors).
>
> I would like to know the reason besides the fact that I passed `true`
> instead of 'checked' - which I believe works just fine.. - and used
> `is(':checked')` instead of `attr('checked')` - which I agree is kinda
> weird.
>
Miller,
we need to distinguish two category of answers here: best Javascript
practices and best jQuery practices.
We must agree that best jQuery practices are probably learned on
jQuery forums where the people that wrote those methods can give a
reasonable answer in case of unexpected behaviors. Here follows my 2
cents about this...
Without looking at the inner code of jQuery we can just guess or read
their documentation about "attr":
http://api.jquery.com/attr/
there is no "property" wording in all their documentation on
".attr()", so it is clear that they are talking about attributes not
properties and the name itself should leave no doubts...
First statement:
$('#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").
Also, the above will not set any "attribute", from what I remember, it
will just set the named property value on the element to boolean true.
Thus the name ".attr()" is itself misplaced and the documentation
misguiding people approaching Javascript and the DOM (read the
comments there).
> I have no idea how jQuery handles those things internally, but depending on
> how the `is(':checked')` is implemented there shouldn't be any
> inconsistencies (if it really checks the proper attribute instead of doing a
> selector query..).
>
> cheers.
Second statement:
$('#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.
--
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]