jQuery attr vs prop

jQuery has the new function prop()as mentioned in their release. And here is what John Resig has to say about it in his blog. Properties and Attributes seem to have quite quite different meanings. An attribute is what is seen in html.

Boolean attributes such as checked only set the default or initial value. In the case of a checkbox, the checked attribute sets whether the checkbox should be checked when the page loads. Properties are what the browser uses to keep track of the current values. Normally, properties reflect their corresponding attributes (if they exist). This is not the case with boolean attributes. Boolean properties stay up to date when the user clicks a checkbox or selects an option in a select element. The corresponding boolean attributes do not. As was stated above, they are used by the browser only to store the initial value.
$(“:checkbox”).get(0).checked = true;
// Is the same as $(":checkbox:first").prop(“checked”, true);
In jQuery 1.6, setting checked with
$(“:checkbox”).attr(“checked”, true);
would not check the checkbox because it was the property that needed to be set and all you were setting was the initial value.
Reference jQuery 1.6 release
Facebook Facebook LinkedIn LinkedIn

Leave a Comment

For Gravatar icon