We’ve come to the first jQuery 1.6 update and are pleased to announce the release of the first release candidate! This code is likely to go to the final version 1.6.1 (it will be released towards the end of the week) - everything, except for (possible) critical bugs, of course.
You can download the code from jQuery CDN:
http://code.jquery.com/jquery-1.6.1rc1.jsYou can contribute to us by placing this code in your working application and letting you know if something stops working. In this case, please make sure that you are testing specifically jQuery 1.6.1 RC 1 and
get a bug .
')
We're going to encourage any
initiative by community members to make useful changes to the jQuery core. We have prepared a whole
page with detailed information designed to simplify this process. Our team is here and ready to help you help us!
Upgrade from 1.5.2 to 1.6.1
The emergence of the
.prop()
method, as well as changes in the
.attr()
method, gave rise to a whole discussion about the differences between attributes and properties and their interrelationships. In addition, there were some problems with backward compatibility, which were fixed in 1.6.1. In other words,
when upgrading from 1.5.2 to 1.6.1, you do not need to change any old code .
Below you will find a description of the changes in the "Attributes" module in jQuery 1.6 and 1.6.1, as well as recommendations on the
preferred use of the
.attr()
and
.prop()
methods. However, again, jQuery 1.6.1 allows you to access the
.attr()
method exactly as you are used to.
List of changes
Recent changes in the Attributes module have eliminated inconsistencies and ambiguities in the behavior of attributes and properties, but at the same time caused confusion in the ranks of the jQuery community, because In all versions of jQuery up to 1.6, a single method (
.attr()
) was used to work with attributes and with properties. The former
.attr()
method contained many bugs and was difficult to maintain.
In jQuery 1.6.1, several bugs were fixed, as well as (again) the Attributes module was updated.
So, boolean attributes (such as checked, selected, readonly and disabled) in 1.6.1 behave exactly the same as in previous versions of jQuery - up to 1.6. Thus, a code like this:
$(“:checkbox”).attr(“ checked ”, true );
$(“option”).attr(“selected”, true );
$(“input”).attr(“ readonly ”, true );
$(“input”).attr(“disabled”, true );
* This source code was highlighted with Source Code Highlighter .
or even this:
if ( $(“:checkbox”).attr(“ checked ”) ) { /* Do something */ }
* This source code was highlighted with Source Code Highlighter .
- you can not change when moving to 1.6.1 and leave it as it is (it will not break anything).
To clarify the essence of the changes made in jQuery 1.6 with respect to the
.attr()
method, we can use several examples that worked in previous versions of jQuery, which now (using 1.6) should have been rewritten:
.attr () | Proper .prop () usage |
$ (window) .attr ... | $ (window) .prop ... |
$ (document) .attr ... | $ (document) .prop ... |
$ (“: Checkbox”). Attr (“checked”, true); | $ (“: Checkbox”). Prop (“checked”, true); |
$ (“Option”). Attr (“selected”, true); | $ (“Option”). Prop (“selected”, true); |
First, calling the
.attr()
method on a window or document will not work in jQuery 1.6, since window and document have no attributes. They contain only properties (for example, location or readyState) that should be manipulated using the
.prop()
method or pure javascript. In jQuery 1.6.1, the
.attr()
method, called on a window or document instead of giving an error, will simply lead to a call to the
.prop()
method.
Further, checked, selected and other above-mentioned attributes are processed in a special way, because there is a special relationship between these attributes and the corresponding properties. In the simplest case, the attribute is what you see in html:
< input type =” checkbox ” checked =” checked ” >
* This source code was highlighted with Source Code Highlighter .
Boolean attributes (for example, checked) set only the default value (during page initialization). In the case of a checkbox, the checked attribute determines whether there will be a check mark in it when the page is loaded.
Properties, in turn, are what the browser reflects changes in
current values. Typically, property values reflect the values of the corresponding attributes (if any). However, in the case of Boolean attributes, everything is different. Boolean properties remain up to date (with the current values) if the user unchecks the check box or selects an item from the drop-down list. The corresponding attribute is not (as already noted above, they are used only to store the default values).
$(“:checkbox”).get(0). checked = true ;
// , $( ":checkbox:first" ).prop(“ checked ”, true );
* This source code was highlighted with Source Code Highlighter .
In jQuery 1.6, this method of dynamically ticking a checkbox:
$(“:checkbox”).attr(“ checked ”, true );
* This source code was highlighted with Source Code Highlighter .
It will not work anymore, because here it was necessary to change the property, not the attribute, i.e. all that we have just achieved is that we have changed the default value.
However, when jQuery 1.6 was released, the jQuery development team realized that this was probably not a very useful feature — setting default values that the browser still reads only once — when the page loads. So, taking into account this fact, as well as in the interests of maintaining backward compatibility, we decided to return the ability to set the values of Boolean attributes using the
.attr()
method in jQuery 1.6.1.
The most common Boolean attributes are checked, selected, disabled, and readOnly, but here’s a complete list of them (attributes and properties that you can dynamically get / set values using
.attr()
method in jQuery 1.6.1):
autofocus, autoplay, async, checked , controls, defer, disabled, hidden, loop, multiple, open, readonly , required, scoped, selected
* This source code was highlighted with Source Code Highlighter .
It is still recommended to use
.prop()
to set boolean attributes / properties, however your old code will continue to work with jQuery 1.6.1 as if nothing had happened.
Below is a list of some attributes and properties with an indication of the method that is recommended to be used to change the value in them (again, this is the
preferred use, the
.attr()
method
.attr()
continue to work in all cases):
Attribute / Property
| .attr ()
| .prop ()
|
accesskey
| ✓
|
|
align
| ✓
|
|
async
| ✓
| ✓
|
autofocus
| ✓
| ✓
|
checked
| ✓
| ✓
|
class
| ✓
|
|
contenteditable
| ✓
|
|
draggable
| ✓
|
|
href
| ✓
|
|
id
| ✓
|
|
label
| ✓
|
|
location (e.g. window.location)
| ✓
| ✓
|
multiple
| ✓
| ✓
|
readOnly
| ✓
| ✓
|
rel
| ✓
|
|
selected
| ✓
| ✓
|
src
| ✓
|
|
tabindex
| ✓
|
|
title
| ✓
|
|
type
| ✓
|
|
width (if you want to .width() )
| ✓
|
|
Neither
.attr()
nor
.prop()
is recommended to be used for writing / reading values - there is
.val()
for this purpose (although
.attr(“value”, “somevalue”)
will still work, like in 1.6).
Total (preferred use)
The
.prop()
method is recommended for working with boolean attributes / properties, as well as with properties that are not present in html (for example, window.location). Manipulations with all other attributes (those that are in html) can (and should) be carried out in the old way using the
.attr()
method.