You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/upgrade-guide/1.9.md
+30-26Lines changed: 30 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ For more information see the [jQuery Migrate plugin](https://github.com/jquery/j
27
27
28
28
The list below does not represent all changes made for jQuery 1.9, just the changes that we anticipate may affect behavior in a way that could break existing code. For a complete and detailed list of changes, see the changelogs in the release announcements on the [jQuery blog](https://blog.jquery.com) or visit [bugs.jquery.com](https://bugs.jquery.com).
29
29
30
-
### .toggle(function, function, ... ) removed
30
+
### .toggle(function, function, ... ) removed
31
31
32
32
This is the "click an element to run the specified functions" signature of `.toggle()`. It should not be confused with the "change the visibility of an element" of `.toggle()` which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.
33
33
@@ -37,11 +37,11 @@ The `jQuery.browser()` method has been deprecated since jQuery 1.3 and is remove
37
37
38
38
### .live() removed
39
39
40
-
The `.live()` method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the `.on()` method instead. To exactly match `$("a.foo").live("click", fn)`, for example, you can write `$(document).on("click", "a.foo", fn)`. For more information, see the [.on() documentation](https://api.jquery.com/on/). In the meantime, the jQuery Migrate plugin can be used to restore the `.live()` functionality.
40
+
The `.live()` method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the `.on()` method instead. To exactly match `$("a.foo").live("click", fn)`, for example, you can write `$(document).on("click", "a.foo", fn)`. For more information, see the [`.on()` documentation](https://api.jquery.com/on/). In the meantime, the jQuery Migrate plugin can be used to restore the `.live()` functionality.
41
41
42
42
### .die() removed
43
43
44
-
The `.die()` method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the `.off()` method instead. To exactly match `$("a.foo").die("click")`, for example, you can write `$(document).off("click", "a.foo")`. For more information, see the [.off() documentation](https://api.jquery.com/off/). In the meantime, the jQuery Migrate plugin can be used to restore the `.die()` functionality.
44
+
The `.die()` method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the `.off()` method instead. To exactly match `$("a.foo").die("click")`, for example, you can write `$(document).off("click", "a.foo")`. For more information, see the [`.off()` documentation](https://api.jquery.com/off/). In the meantime, the jQuery Migrate plugin can be used to restore the `.die()` functionality.
45
45
46
46
### jQuery.sub() removed
47
47
@@ -53,52 +53,56 @@ The `.add()` method is always supposed to return its results in document order.
53
53
54
54
### .addBack( selector ) replaces .andSelf()
55
55
56
-
As of jQuery 1.8, the `.andSelf()` method was deprecated in favor of the `.addBack()` method, which we feel is a better name for what this method does--"add back" the previous set of results. The new method accepts an optional selector that can be used to filter the previous set before adding it to the current set. So, `$("section, aside").children("ul").addBack("aside")` results in a set that includes all `aside` nodes plus the `ul` children of both `section` and `aside` nodes, in document order. Although the `.andSelf()` method still works in 1.9, we recommend switching names as soon as possible. The jQuery Migrate plugin will warn about the use of `.andSelf()`.
56
+
As of jQuery 1.8, the `.andSelf()` method was deprecated in favor of the `.addBack()` method, which we feel is a better name for what this method does--"add back" the previous set of results. The new method accepts an optional selector that can be used to filter the previous set before adding it to the current set. So, `$("section, aside").children("ul").addBack("aside")` results in a set that includes all `aside` nodes plus the `ul` children of both `section` and `aside` nodes, in document order. Although the `.andSelf()` method still works in 1.9, we recommend switching names as soon as possible. The jQuery Migrate plugin will warn about the use of `.andSelf()`.
57
57
58
58
### .after(), .before(), and .replaceWith() with disconnected nodes
59
59
60
60
Prior to 1.9, `.after()`, `.before()`, and `.replaceWith()` would attempt to add or change nodes in the current jQuery set if the first node in the set was not connected to a document, and in those cases return a new jQuery set rather than the original set. This created several inconsistencies and outright bugs--the method might or might not return a new result depending on its arguments! As of 1.9, these methods always return the original unmodified set and attempting to use `.after()`, `.before()`, or `.replaceWith()` on a node without a parent has no effect--that is, neither the set or the nodes it contains are changed.
61
61
62
62
### .appendTo, .insertBefore, .insertAfter, and .replaceAll
63
63
64
-
As of 1.9, these methods _always_ return a new set, making them consistently usable with chaining and the `.end()` method. Prior to 1.9, they would return the old set only if there was a single target element. Note that these methods have always returned the aggregate set of all elements appended to the target elements. If no elements are selected by the target selector (e.g., `$(elements).appendTo("#not_found")`) the resulting set will be empty.
64
+
As of 1.9, these methods _always_ return a new set, making them consistently usable with chaining and the `.end()` method. Prior to 1.9, they would return the old set only if there was a single target element. Note that these methods have always returned the aggregate set of all elements appended to the target elements. If no elements are selected by the target selector (e.g., `$(elements).appendTo("#not_found")`) the resulting set will be empty.
65
65
66
66
### Ajax events should be attached to document
67
67
68
-
As of jQuery 1.9, the global Ajax events (ajaxStart, ajaxStop, ajaxSend, ajaxComplete, ajaxError, and ajaxSuccess) are only triggered on the `document` element. Change the program to listen for the Ajax events on the document. For example, if the code currently looks like this:
As of jQuery 1.9, the global Ajax events (`ajaxStart`, `ajaxStop`, `ajaxSend`, `ajaxComplete`, `ajaxError`, and `ajaxSuccess`) are only triggered on the `document` element. Change the program to listen for the Ajax events on the document. For example, if the code currently looks like this:
### Checkbox/radio state in a .trigger()ed "click" event
78
82
79
-
When the _user_ clicks on a checkbox or radio button, the event handler sees the node in the state it will be in if `event.preventDefault()` is not called on the node--in essence, its new state. So for example, if the user clicks on an unchecked checkbox, the event handler will see a _checked_ box. Before 1.9, a synthetic event triggered by either `.trigger("click")` or `.click()` would see the checkbox in the opposite state than that of a user action. This has been fixed in 1.9 to reflect the same checked state as a user-initiated action.
83
+
When the _user_ clicks on a checkbox or radio button, the event handler sees the node in the state it will be in if `event.preventDefault()` is not called on the node--in essence, its new state. So for example, if the user clicks on an unchecked checkbox, the event handler will see a _checked_ box. Before 1.9, a synthetic event triggered by either `.trigger("click")` or `.click()` would see the checkbox in the opposite state than that of a user action. This has been fixed in 1.9 to reflect the same checked state as a user-initiated action.
80
84
81
85
### Order of triggered "focus" events
82
86
83
-
When the user clicks or tabs into a form element to bring it into focus, the browser first fires a blur event for the previously focused element and then a focus event for the new element. Prior to 1.9, a trigger()ed focus event using either `.trigger("focus")` or `.focus()` would fire a focus event for the new element and then the blur event for the previous element before finally actually focusing the element. In 1.9 this behavior has been changed to reflect the same order as if the user had caused the focus change.
87
+
When the user clicks or tabs into a form element to bring it into focus, the browser first fires a blur event for the previously focused element and then a focus event for the new element. Prior to 1.9, a trigger()ed focus event using either `.trigger("focus")` or `.focus()` would fire a focus event for the new element and then the blur event for the previous element before finally actually focusing the element. In 1.9 this behavior has been changed to reflect the same order as if the user had caused the focus change.
84
88
85
-
With native DOM focus events, the browser only calls a focus event handler if the target element is not already focused and can also successfully be focused. jQuery has always ensured that a call to `.trigger("focus")` or `.focus()` consistently runs any attached event handlers, even if the element cannot be focused, and jQuery 1.9 continues to do that. This is different behavior than the DOM `.focus()` method, which will not call event handlers in many cases including where the element is already focused or the element is disabled.
89
+
With native DOM focus events, the browser only calls a focus event handler if the target element is not already focused and can also successfully be focused. jQuery has always ensured that a call to `.trigger("focus")` or `.focus()` consistently runs any attached event handlers, even if the element cannot be focused, and jQuery 1.9 continues to do that. This is different behavior than the DOM `.focus()` method, which will not call event handlers in many cases including where the element is already focused or the element is disabled.
86
90
87
-
Unfortunately, all versions of Internet Explorer (6 through 10) fire focus events asynchronously. When you `.trigger("focus")` in IE, jQuery won't "see" the async focus event which will occur later, so it fires one of its own to ensure that a focus event always occurs as described above. This causes two calls to the event handler. To avoid this double-call--but risk that the event handler is not called at all--use the DOM focus method directly, e.g., `$("selector").get(0).focus()`.
91
+
Unfortunately, all versions of Internet Explorer (6 through 10) fire focus events asynchronously. When you `.trigger("focus")` in IE, jQuery won't "see" the async focus event which will occur later, so it fires one of its own to ensure that a focus event always occurs as described above. This causes two calls to the event handler. To avoid this double-call--but risk that the event handler is not called at all--use the DOM focus method directly, e.g., `$("selector").get( 0 ).focus()`.
88
92
89
-
### jQuery(htmlString) versus jQuery(selectorString)
93
+
### jQuery(htmlString) versus jQuery(selectorString)
90
94
91
95
Prior to 1.9, a string would be considered to be an HTML string if it had HTML tags anywhere within the string. This has the potential to cause inadvertent execution of code and reject valid selector strings. As of 1.9, a string is only considered to be HTML if it starts with a less-than ("`<`") character. The Migrate plugin can be used to restore the pre-1.9 behavior.
92
96
93
-
If a string is known to be HTML but may start with arbitrary text that is not an HTML tag, pass it to `jQuery.parseHTML()` which will return an array of DOM nodes representing the markup. A jQuery collection can be created from this, for example: `$($.parseHTML(htmlString))`. This would be considered best practice when processing HTML templates for example. Simple uses of literal strings such as `$("<p>Testing</p>").appendTo("body")` are unaffected by this change.
97
+
If a string is known to be HTML but may start with arbitrary text that is not an HTML tag, pass it to `jQuery.parseHTML()` which will return an array of DOM nodes representing the markup. A jQuery collection can be created from this, for example: `$($.parseHTML(htmlString ) )`. This would be considered best practice when processing HTML templates for example. Simple uses of literal strings such as `$("<p>Testing</p>").appendTo("body")` are unaffected by this change.
94
98
95
99
Bottom line: HTML strings passed to `jQuery()` that start with something other than a less-than character will be interpreted as a selector. Since the string usually cannot be interpreted as a selector, the most likely result will be an "invalid selector syntax" error thrown by the Sizzle selector engine. Use `jQuery.parseHTML()` to parse arbitrary HTML.
96
100
97
101
When the jQuery Migrate plugin is used, it will use the old rules for determining if the string passed to `$()` "looks like HTML".
98
102
99
103
### Events not fired by the .data() method; names with periods
100
104
101
-
The `.data()` method had an undocumented and incredibly non-performant way to monitor setting and getting of values that was removed in 1.9. This has affected the interpretation of data names that contain periods, in a good way. As of 1.9, a call to `.data("abc.def")` retrieves the data for the name "abc.def" _only_, and never just "abc". Note that the lower-level `jQuery.data()` method never supported events and so it has not changed. The jQuery Migrate plugin does _not_ restore the old behavior for this case.
105
+
The `.data()` method had an undocumented and incredibly non-performant way to monitor setting and getting of values that was removed in 1.9. This has affected the interpretation of data names that contain periods, in a good way. As of 1.9, a call to `.data("abc.def")` retrieves the data for the name "abc.def" _only_, and never just "abc". Note that the lower-level `jQuery.data()` method never supported events and so it has not changed. The jQuery Migrate plugin does _not_ restore the old behavior for this case.
102
106
103
107
### Ordering of disconnected nodes within a jQuery set
104
108
@@ -124,14 +128,14 @@ Here are some examples of correct usage when setting `checked` on a checkbox; th
124
128
125
129
```js
126
130
// Correct if changing the attribute is desired
127
-
$(elem).attr("checked", "checked");
131
+
$(elem).attr("checked", "checked");
128
132
// Correct for checking the checkbox
129
-
$(elem).prop("checked", true);
133
+
$(elem).prop("checked", true);
130
134
131
135
// Correct if removing the attribute is desired (rare)
132
-
$(elem).removeAttr("checked");
136
+
$(elem).removeAttr("checked");
133
137
// Correct for clearing the checkbox
134
-
$(elem).prop("checked", false);
138
+
$(elem).prop("checked", false);
135
139
```
136
140
137
141
The `value` property versus attribute on `input` elements is another example of this ambiguity. The attribute generally reflects the value that was read from the HTML markup; the property reflects the current value. Since the `.val()` method is the recommended jQuery way to get or set the values of form elements, this confusion usually does not affect users.
@@ -140,7 +144,7 @@ However, when a selector like `"input[value=abc]"` is used, it should always sel
140
144
141
145
The jQuery Migrate plugin restores the old attribute-vs-property rules.
142
146
143
-
### $("input").attr("type", newValue) in oldIE
147
+
### $("input").attr("type", newValue) in oldIE
144
148
145
149
Prior to version 1.9, jQuery would throw an exception in all browsers for any attempt to set the `type` attribute on an input or button element. This was done to accommodate the lowest common denominator; IE 6/7/8 throw an error if you attempt to change the type of an input element. As of jQuery 1.9, we allow you to set the type of an element if the browser allows it. However, your own code will need to be aware that attempting to do this on oldIE will still throw an error. The jQuery Migrate plugin warns when you attempt to set the `type` attribute but does not throw a JavaScript error.
146
150
@@ -154,19 +158,19 @@ The remaining purpose of the deprecated `.selector` property on a jQuery object
154
158
155
159
### jQuery.attr()
156
160
157
-
In 1.9 we have removed the undocumented signature jQuery.attr(elem, name, value, pass) using the pass argument. Any code that depended on this should be rewritten, but the jQuery Migrate plugin can provide backward-compatible behavior and will warn if this signature is used.
161
+
In 1.9 we have removed the undocumented signature `jQuery.attr(elem, name, value, pass )` using the pass argument. Any code that depended on this should be rewritten, but the jQuery Migrate plugin can provide backward-compatible behavior and will warn if this signature is used.
158
162
159
163
### jQuery.ajax returning a JSON result of an empty string
160
164
161
165
Prior to 1.9, an ajax call that expected a return data type of JSON or JSONP would consider a return value of an empty string to be a success case, but return a null to the success handler or promise. As of 1.9, an empty string returned for JSON data is considered to be malformed JSON (because it is); this will now throw an error. Use the error handler to catch such cases.
162
166
163
167
### jQuery.proxy() context
164
168
165
-
New in 1.9, the function returned by calling jQuery.proxy with a falsy context will preserve its `this` object for the provided function. Previously, a falsy value for context would get translated into the global object (window) if null/undefined, or else wrapped in an object (e.g., new Boolean(false)).
169
+
New in 1.9, the function returned by calling jQuery.proxy with a falsy context will preserve its `this` object for the provided function. Previously, a falsy value for context would get translated into the global object (window) if null/undefined, or else wrapped in an object (e.g., `new Boolean(false )`).
166
170
167
-
### .data("events")
171
+
### .data("events")
168
172
169
-
Prior to 1.9, `.data("events")` could be used to retrieve jQuery's undocumented internal event data structure for an element if no other code had defined a data element with the name "events". This special case has been removed in 1.9. There is no public interface to retrieve this internal data structure, and it remains undocumented. However, the jQuery Migrate plugin restores this behavior for code that depends upon it.
173
+
Prior to 1.9, `.data("events")` could be used to retrieve jQuery's undocumented internal event data structure for an element if no other code had defined a data element with the name "events". This special case has been removed in 1.9. There is no public interface to retrieve this internal data structure, and it remains undocumented. However, the jQuery Migrate plugin restores this behavior for code that depends upon it.
0 commit comments