you are right Diego, sorry for the misunderstanding i thought that you where saying that that would not work. and yes it does not modify the attribute it modifies the property.
On Jan 18, 2011, at 8:48 PM, Diego Perini wrote: > On Wed, Jan 19, 2011 at 12:23 AM, fernando trasvina <[email protected]> > wrote: >> >> On Jan 18, 2011, at 2:04 PM, Garrett Smith wrote: >> >>> On 1/17/11, 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. >>>> >>>> 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..). >> >> because any truthy value passed checks the input so 'checked' is truthy >> >> Also jQuery works fine setting the value via .attr method and sets the >> correct value in the correct object and property >> >>>> >>> DOn't know what you mean. >>> >>> The important thing to remember: Attributes != properties. >>> >>> el.getAttribute("checked"); // attribute value. >>> el.checked; // property value. >>> >>> In most cases you'll want the property value, so just use that. A >>> wrapper that tries to abstract reading all attributes or all >>> properties from every element in all browsers will be complicated and >>> probably have problems in some cases. >>> -- >>> Garrett >>> >>> -- >>> 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] >> > > Fernando... trying again… with some language barriers but with good mood :) > > To make things easier let's start by forgetting about Internet > Explorer for a moment and for the following example let's "assume" our > objective is a standard compliant browsers. > > What I have stated above is that the ".attr()" name was badly chosen > to describe the effects achieved to normal readers. I added that the > documentation also let readers understand that the method is aimed at > changing the HTML attribute of an element, not the property having the > same name. > > I haven't said the ".attr()" is unable to change the visual state of a > checkbox, I just pointed out that .attr()" is not going to change any > attribute as it seems or as it can be read in the docs. > > So let's take one problem at a time, separately. Here I want to show > how I see this being misinterpreted, thus enforcing the fact that the > documentation talks about "attributes" being changed while the truth > is no HTML attribute are changed for the element. > > I tried this in latest Firefox 3.6 with two HTML checkboxes and the > Firebug console open: > > <html> > <head> > <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> > <title>Test dynamic attributes and properties using jQuery</title> > <script type="text/javascript" src="jquery-1.4.4.js"></script> > </head> > <body> > <form action=""> > <input id="jq-test1" type="checkbox" checked /> > <input id="jq-test2" type="checkbox" /> > </form> > </body> > </html> > > The following lines (those marked with `>>>`) are the statements I > typed in Firebug console, next lines are the results: > >>>> $("#jq-test1").attr("checked"); > true >>>> $("#jq-test1")[0] > <input id="jq-test1" type="checkbox" checked=""> > >>>> $("#jq-test1[checked]").attr("checked", false); > [input#jq-test1] > >>>> $("#jq-test1").attr("checked"); > false >>>> $("#jq-test1")[0] > <input id="jq-test1" type="checkbox" checked=""> > > as you can see, the ".attr("checked", false)" may have changed a ton > of things on the element, but it surely didn't change any HTML > attribute on it. > > I repeat, false/true may work to switch on/of the visual check and the > properties bound to that behavior but that wasn't the point, was it ? > > If we can at least agree on the fact that ".attr()" does not change > any HTML attributes in this specific case it will be a step forward. > > > -- > Diego > > > PS: I also tried to remove the attribute using jQuery method > ".removeAttr()" cited in a previous posts but it doesn't seem to > remove the HTML attribute from the element :) > > -- > 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]
