diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 000000000..8b808995d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,13 @@
+# editorconfig.org
+root = true
+
+[*]
+indent_style = space
+indent_size = 2
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.js, *.xsl]
+indent_style = tab
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 000000000..b7ca95b5b
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,5 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# JS files must always use LF for tools to work
+*.js eol=lf
diff --git a/.github/configs/hydra-config.json b/.github/configs/hydra-config.json
new file mode 100644
index 000000000..2173722b4
--- /dev/null
+++ b/.github/configs/hydra-config.json
@@ -0,0 +1,10 @@
+{
+ "//": [
+ "2023-05: twitter.com serves broken redirect-loop",
+ "2025-04: The oembed endpoint responds HTTP 429 Too Many Requests too often; perhaps because almost every page links to one"
+ ],
+ "exclude_scheme_prefixes": [
+ "https://twitter.com/",
+ "https://api.jquery.com/wp-json/oembed/1.0/embed"
+ ]
+}
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 000000000..aa2f74565
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,13 @@
+version: 2
+updates:
+ - package-ecosystem: github-actions
+ directory: "/"
+ schedule:
+ interval: monthly
+
+ # Group all dependabot version update PRs into one
+ groups:
+ github-actions:
+ applies-to: version-updates
+ patterns:
+ - "*"
diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
new file mode 100644
index 000000000..30412870c
--- /dev/null
+++ b/.github/workflows/node.js.yml
@@ -0,0 +1,32 @@
+# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
+
+name: Node.js CI
+
+on:
+ pull_request:
+ push:
+ branches-ignore: "dependabot/**"
+
+jobs:
+ build:
+
+ runs-on: ubuntu-latest
+
+ strategy:
+ matrix:
+ node-version: [18.x, 20.x]
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
+ - name: Update apt-get cache
+ run: sudo apt-get update
+ - name: Install xmllint
+ run: sudo apt-get install -y libxml2-utils
+ - name: Use Node.js ${{ matrix.node-version }}
+ uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
+ with:
+ node-version: ${{ matrix.node-version }}
+ cache: 'npm'
+ - run: npm ci
+ - run: npm test
diff --git a/.github/workflows/spider-check.yaml b/.github/workflows/spider-check.yaml
new file mode 100644
index 000000000..95ea1ea24
--- /dev/null
+++ b/.github/workflows/spider-check.yaml
@@ -0,0 +1,32 @@
+name: spider-check
+on:
+ # Once a week on Friday at 00:00
+ schedule:
+ - cron: '0 0 * * 5'
+ # Or manually
+ workflow_dispatch:
+ # Or when developing this workflow
+ push:
+ paths:
+ - .github/workflows/spider-check.yaml
+ - .github/configs/hydra-config.json
+ pull_request:
+ paths:
+ - .github/workflows/spider-check.yaml
+ - .github/configs/hydra-config.json
+
+jobs:
+ spider-check:
+ if: ${{ github.repository_owner == 'jquery' }} # skip on forks
+ runs-on: ubuntu-latest
+ env:
+ # Site address to crawl
+ MY_SITE: https://api.jquery.com
+ steps:
+ - name: Checkout
+ uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
+
+ - name: Run hydra-link-checker
+ run: |
+ curl -O https://raw.githubusercontent.com/jquery/hydra-link-checker/v2.0.0/hydra.py
+ python3 hydra.py "$MY_SITE" --config .github/configs/hydra-config.json
diff --git a/.github/workflows/typesense.yaml b/.github/workflows/typesense.yaml
new file mode 100644
index 000000000..de973410d
--- /dev/null
+++ b/.github/workflows/typesense.yaml
@@ -0,0 +1,30 @@
+name: typesense
+on:
+ # Once a day at 11:30 UTC .attr()
, .html()
, and .val()
—also act as "getters," retrieving information from DOM elements for later use.
+ ]]>
\\
. For example, an element with id="foo.bar"
, can use the selector $("#foo\\.bar")
. The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~
) as a literal part of a name, it must be escaped with with two backslashes: \\
. For example, an element with id="foo.bar"
, can use the selector $("#foo\\.bar")
. The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.
<a href="example.html" rel="nofollow">Some text</a>
but not <a href="example.html" rel="nofollow foe">Some text</a>
.
- Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.
+Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.
$('a[rel="nofollow self"]')
$("a[rel='nofollow self']")
$(selector, context)
method.Given a jQuery object that represents a set of DOM elements, the .add()
method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to .add()
can be pretty much anything that $()
accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.
Do not assume that this method appends the elements to the existing collection in the order they are passed to the .add()
method. When all elements are members of the same document, the resulting collection from .add()
will be sorted in document order; that is, in order of each element's appearance in the document. If the collection consists of elements from different documents or ones not in any document, the sort order is undefined. To create a jQuery object with elements in a well-defined order, use the $(array_of_DOM_elements)
signature.
Do not assume that this method appends the elements to the existing collection in the order they are passed to the .add()
method. When all elements are members of the same document, the resulting collection from .add()
will be sorted in document order; that is, in order of each element's appearance in the document. If the collection consists of elements from different documents or ones not in any document, the sort order is undefined. To create a jQuery object with elements in a well-defined order and without sorting overhead, use the $(array_of_DOM_elements)
signature.
The updated set of elements can be used in a following (chained) method, or assigned to a variable for later use. For example:
$( "p" ).add( "div" ).addClass( "widget" );
diff --git a/entries/addClass.xml b/entries/addClass.xml
index 7d299dba9..39d6a0bf5 100644
--- a/entries/addClass.xml
+++ b/entries/addClass.xml
@@ -7,15 +7,36 @@
One or more space-separated classes to be added to the class attribute of each matched element.
+
+ 3.3
+
+ An array of classes to be added to the class attribute of each matched element.
+
+
1.4
-
+
A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, this
refers to the current element in the set.
+
+
+
- Adds the specified class(es) to each of the set of matched elements.
+
+ 3.3
+
+ A function returning one or more space-separated class names or an array of class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, this
refers to the current element in the set.
+
+
+
+
+
+
+ Adds the specified class(es) to each element in the set of matched elements.
It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.
+ Before jQuery version 1.12/2.2, the .addClass()
method manipulated the className
property of the selected elements, not the class
attribute. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
+ As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class
attribute is used instead. So, .addClass()
can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
$( "p" ).addClass( "myClass yourClass" );
@@ -59,7 +80,30 @@ $( "p" ).last().addClass( "selected" );
Add the classes "selected" and "highlight" to the matched elements.
+
+
+
+
+ Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
+
Since .after()
can accept any number of additional arguments, the same result can be achieved by passing in the three <div>
s as three separate arguments, like so: $( "p" ).first().after( $newdiv1, newdiv2, existingdiv1 )
. The type and number of arguments will largely depend on the elements that are collected in the code.
+
+
Inserts some HTML after all paragraphs.
+
diff --git a/entries/ajaxError-shorthand.xml b/entries/ajaxError-shorthand.xml
new file mode 100644
index 000000000..f36ac54a8
--- /dev/null
+++ b/entries/ajaxError-shorthand.xml
@@ -0,0 +1,19 @@
+
+
+ .ajaxError()
+ Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
+
+ 1.0
+
+ The function to be invoked.
+
+
+
+
+ This API is deprecated. Use .on( "ajaxError", handler )
instead.
+
+
+
+
+
+
diff --git a/entries/ajaxError.xml b/entries/ajaxError.xml
index 801fef62c..d18cf9775 100644
--- a/entries/ajaxError.xml
+++ b/entries/ajaxError.xml
@@ -1,15 +1,25 @@
-
- .ajaxError()
+
+ ajaxError event
+ Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
- 1.0
-
+ 1.7
+
+ The string "ajaxError"
.
+
+
The function to be invoked.
+
+
+
+
- Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
- Whenever an Ajax request completes with an error, jQuery triggers the ajaxError
event. Any and all handlers that have been registered with the .ajaxError()
method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.
+
+ This page describes the ajaxError
event. For the deprecated .ajaxError()
method, see .ajaxError()
.
+
+ Whenever an Ajax request completes with an error, jQuery triggers the ajaxError
event. Any and all registered ajaxError
handlers are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.
To observe this method in action, set up a basic Ajax load request.
<button class="trigger">Trigger</button>
@@ -18,35 +28,37 @@
Attach the event handler to the document:
-$( document ).ajaxError(function() {
- $( "div.log" ).text( "Triggered ajaxError handler." );
-});
+$( document ).on( "ajaxError", function() {
+ $( ".log" ).text( "Triggered ajaxError handler." );
+} );
Now, make an Ajax request using any jQuery method:
$( "button.trigger" ).on( "click", function() {
$( "div.result" ).load( "ajax/missing.html" );
-});
+} );
When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.
- As of jQuery 1.8, the .ajaxError()
method should only be attached to document
.
All ajaxError
handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, use the parameters passed to the handler. Each time an ajaxError
handler is executed, it is passed the event object, the jqXHR
object (prior to jQuery 1.5, the XHR
object), and the settings object that was used in the creation of the request. When an HTTP error occurs, the fourth argument (thrownError
) receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." For example, to restrict the error callback to only handling events dealing with a particular URL:
-$( document ).ajaxError(function( event, jqxhr, settings, exception ) {
+$( document ).on( "ajaxError", function( event, jqxhr, settings, thrownError ) {
if ( settings.url == "ajax/missing.html" ) {
$( "div.log" ).text( "Triggered ajaxError handler." );
}
-});
+} );
+
This API is deprecated. Use .on( "ajaxSend", handler )
instead.
"ajaxSend"
.Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend
event. Any and all handlers that have been registered with the .ajaxSend()
method are executed at this time.
This page describes the ajaxSend
event. For the deprecated .ajaxSend()
method, see .ajaxSend()
.
Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend
event. Any and all registerd ajaxSend
handlers are executed at this time.
To observe this method in action, set up a basic Ajax load request:
<div class="trigger">Trigger</div>
@@ -18,36 +27,37 @@
Attach the event handler to the document:
-$( document ).ajaxSend(function() {
+$( document ).on( "ajaxSend", function() {
$( ".log" ).text( "Triggered ajaxSend handler." );
-});
+} );
Now, make an Ajax request using any jQuery method:
-$( ".trigger" ).click(function() {
+$( ".trigger" ).on( "click", function() {
$( ".result" ).load( "ajax/test.html" );
-});
+} );
When the user clicks the element with class trigger
and the Ajax request is about to begin, the log message is displayed.
As of jQuery 1.8, the .ajaxSend()
method should only be attached to document
.
All ajaxSend
handlers are invoked, regardless of what Ajax request is to be sent. If you must differentiate between the requests, use the parameters passed to the handler. Each time an ajaxSend
handler is executed, it is passed the event object, the jqXHR
object (in version 1.4, XMLHttpRequest
object), and the settings object that was used in the creation of the Ajax request. For example, you can restrict the callback to only handling events dealing with a particular URL:
-$( document ).ajaxSend(function( event, jqxhr, settings ) {
+$( document ).on( "ajaxSend", function( event, jqxhr, settings ) {
if ( settings.url == "ajax/test.html" ) {
$( ".log" ).text( "Triggered ajaxSend handler." );
}
-});
+} );
This API is deprecated. Use .on( "ajaxStart", handler )
instead.
"ajaxStart"
.Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart
event. Any and all handlers that have been registered with the .ajaxStart()
method are executed at this time.
This page describes the ajaxStart
event. For the deprecated .ajaxStart()
method, see .ajaxStart()
.
Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart
event. Any and all handlers that have been registered with .on( "ajaxStart", ... )
are executed at this time.
To observe this method in action, set up a basic Ajax load request:
<div class="trigger">Trigger</div>
@@ -18,28 +24,29 @@
Attach the event handler to any element:
-$( document ).ajaxStart(function() {
+$( document ).on( "ajaxStart", function() {
$( ".log" ).text( "Triggered ajaxStart handler." );
-});
+} );
Now, make an Ajax request using any jQuery method:
-$( ".trigger" ).click(function() {
+$( ".trigger" ).on( "click", function() {
$( ".result" ).load( "ajax/test.html" );
-});
+} );
When the user clicks the element with class trigger
and the Ajax request is sent, the log message is displayed.
As of jQuery 1.8, the .ajaxStart()
method should only be attached to document
.
This API is deprecated. Use .on( "ajaxStop", handler )
instead.
"ajaxStop"
.Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop
event. Any and all handlers that have been registered with the .ajaxStop()
method are executed at this time. The ajaxStop
event is also triggered if the last outstanding Ajax request is cancelled by returning false within the beforeSend
callback function.
This page describes the ajaxStop
event. For the deprecated .ajaxStop()
method, see .ajaxStop()
.
Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop
event. Any and all registered ajaxStop
handlers are executed at this time. The ajaxStop
event is also triggered if the last outstanding Ajax request is cancelled by returning false within the beforeSend
callback function.
To observe this method in action, set up a basic Ajax load request:
<div class="trigger">Trigger</div>
@@ -18,28 +24,29 @@
Attach the event handler to the document:
-$( ".log" ).ajaxStop(function() {
- $( this ).text( "Triggered ajaxStop handler." );
-});
+$( document ).on( "ajaxStop", function() {
+ $( ".log" ).text( "Triggered ajaxStop handler." );
+} );
Now, make an Ajax request using any jQuery method:
-$( ".trigger" ).click(function() {
+$( ".trigger" ).on( "click", function() {
$( ".result" ).load( "ajax/test.html" );
-});
+} );
When the user clicks the element with class trigger
and the Ajax request completes, the log message is displayed.
As of jQuery 1.8, the .ajaxStop()
method should only be attached to document
.
This API is deprecated. Use .on( "ajaxSuccess", handler )
instead.
"ajaxSuccess"
.Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess
event. Any and all handlers that have been registered with the .ajaxSuccess()
method are executed at this time.
This page describes the ajaxSuccess
event. For the deprecated .ajaxSuccess()
method, see .ajaxSuccess()
.
Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess
event. Any and all registered ajaxSuccess
handlers are executed at this time.
To observe this method in action, set up a basic Ajax load request:
<div class="trigger">Trigger</div>
@@ -18,38 +28,39 @@
Attach the event handler to any element:
-$(document).ajaxSuccess(function() {
+$( document ).on( "ajaxSuccess", function() {
$( ".log" ).text( "Triggered ajaxSuccess handler." );
-});
+} );
Now, make an Ajax request using any jQuery method:
$( ".trigger" ).on( "click", function() {
$( ".result" ).load( "ajax/test.html" );
-});
+} );
When the user clicks the element with class trigger
and the Ajax request completes successfully, the log message is displayed.
As of jQuery 1.8, the .ajaxSuccess()
method should only be attached to document
.
All ajaxSuccess
handlers are invoked, regardless of what Ajax request was completed. If you must differentiate between the requests, you can use the parameters passed to the handler. Each time an ajaxSuccess
handler is executed, it is passed the event object, the XMLHttpRequest
object, and the settings object that was used in the creation of the request. For example, you can restrict the callback to only handling events dealing with a particular URL:
-$( document ).ajaxSuccess(function( event, xhr, settings ) {
+$( document ).on( "ajaxSuccess", function( event, xhr, settings ) {
if ( settings.url == "ajax/test.html" ) {
- $( ".log" ).text( "Triggered ajaxSuccess handler. The ajax response was: " +
+ $( ".log" ).text( "Triggered ajaxSuccess handler. The Ajax response was: " +
xhr.responseText );
}
-});
+} );
- Note: You can get the returned ajax contents by looking at xhr.responseXML
or xhr.responseText
for xml and html respectively.
Note: You can get the returned Ajax contents by looking at xhr.responseXML
or xhr.responseText
for xml and html respectively.
Note: This function has been deprecated and is now an alias for .addBack()
, which should be used with jQuery 1.8 and later.
As described in the discussion for .end()
, jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, .andSelf()
can help.
Consider a page with a simple list on it:
-
-<ul>
- <li>list item 1</li>
- <li>list item 2</li>
- <li class="third-item">list item 3</li>
- <li>list item 4</li>
- <li>list item 5</li>
-</ul>
-
- The result of the following code is a red background behind items 3, 4 and 5:
-
-$( "li.third-item" ).nextAll().andSelf()
- .css( "background-color", "red" );
-
- First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to .nextAll()
then pushes the set of items 4 and 5 onto the stack. Finally, the .andSelf()
invocation merges these two sets together, creating a jQuery object that points to all three items in document order: [ <li.third-item>, <li>, <li> ]
.
Note: This API has been removed in jQuery 3.0; use .addBack()
instead, which should work identically.
.andSelf()
method causes the previous set of DOM elements in the traversal stack to be added to the current set. In the first example, the top stack contains the set resulting from .find("p")
. In the second example, .andSelf()
adds the previous set of elements on the stack — in this case $( "div.after-andself" )
— to the current set, selecting both the div and its enclosed paragraphs.
- The .animate()
method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties. This object is similar to the one that can be sent to the .css()
method, except that the range of properties is more restrictive.
All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width
, height
, or left
can be animated but background-color
cannot be, unless the jQuery.Color() plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units em
and %
can be specified where applicable.
All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width
, height
, or left
can be animated but background-color
cannot be, unless the jQuery.Color plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units em
and %
can be specified where applicable.
In addition to style properties, some non-style properties such as scrollTop
and scrollLeft
, as well as custom properties, can be animated.
Shorthand CSS properties (e.g. font, background, border) are not fully supported. For example, if you want to animate the rendered border width, at least a border style and border width other than "auto" must be set in advance. Or, if you want to animate font size, you would use fontSize
or the CSS equivalent 'font-size'
rather than simply 'font'
.
In addition to numeric values, each property can take the strings 'show'
, 'hide'
, and 'toggle'
. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element. In order to use jQuery's built-in toggle state tracking, the 'toggle'
keyword must be consistently given as the value of the property being animated.
Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The default duration is 400
milliseconds. The strings 'fast'
and 'slow'
can be supplied to indicate durations of 200
and 600
milliseconds, respectively.
If supplied, the complete
callback function is fired once the animation is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but this
is set to the DOM element being animated. If multiple elements are animated, the callback is executed once per matched element, not once for the animation as a whole.
If supplied, the start
, step
, progress
, complete
, done
, fail
, and always
callbacks are called on a per-element basis; this
is set to the DOM element being animated. If no elements are in the set, no callbacks are called. If multiple elements are animated, the callback is executed once per matched element, not once for the animation as a whole. Use the .promise()
method to obtain a promise to which you can attach callbacks that fire once for an animated set of any size, including zero elements.
To animate any element, such as a simple image:
@@ -45,7 +45,7 @@
To animate the opacity, left offset, and height of the image simultaneously:
-$( "#clickme" ).click(function() {
+$( "#clickme" ).on( "click", function() {
$( "#book" ).animate({
opacity: 0.25,
left: "+=50",
@@ -56,20 +56,26 @@ $( "#clickme" ).click(function() {
});
-
+
+
Note that the target value of the height
property is 'toggle'
. Since the image was visible before, the animation shrinks the height to 0 to hide it. A second click then reverses this transition:
-
+
+
The opacity
of the image is already at its target value, so this property is not animated by the second click. Since the target value for left
is a relative value, the image moves even farther to the right during this second animation.
Directional properties (top
, right
, bottom
, left
) have no discernible effect on elements if their position
style property is static
, which it is by default.
Note: The jQuery UI project extends the .animate()
method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.
Note: The jQuery UI project extends the .animate()
method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.
Note: if attempting to animate an element with a height or width of 0px, where contents of the element are visible due to overflow, jQuery may clip this overflow during animation. By fixing the dimensions of the original element being hidden however, it is possible to ensure that the animation runs smoothly. A clearfix can be used to automatically fix the dimensions of your main element without the need to set this manually.
+Note: if attempting to animate an element with a height or width of 0px, where contents of the element are visible due to overflow, jQuery may clip this overflow during animation. By fixing the dimensions of the original element being hidden however, it is possible to ensure that the animation runs smoothly. A clearfix can be used to automatically fix the dimensions of your main element without the need to set this manually.
The second version of .animate()
provides a step
option — a callback function that is fired at each step of the animation. This function is useful for enabling custom animation types or altering the animation as it is occurring. It accepts two arguments (now
and fx
), and this
is set to the DOM element being animated.
The remaining parameter of .animate()
is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing
, and one that progresses at a constant pace, called linear
. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.
The remaining parameter of .animate()
is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing
, and one that progresses at a constant pace, called linear
. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.
As of jQuery version 1.4, you can set per-property easing functions within a single .animate()
call. In the first version of .animate()
, each property can take an array as its value: The first member of the array is the CSS property and the second member is an easing function. If a per-property easing function is not defined for a particular property, it uses the value of the .animate()
method's optional easing argument. If the easing argument is not defined, the default swing
function is used.
For example, to simultaneously animate the width and height with the swing
easing function and the opacity with the linear
easing function:
-$( "#clickme" ).click(function() {
+$( "#clickme" ).on( "click", function() {
$( "#book" ).animate({
width: [ "toggle", "swing" ],
height: [ "toggle", "swing" ],
@@ -107,7 +113,7 @@ $( "#clickme" ).click(function() {
In the second version of .animate()
, the options object can include the specialEasing
property, which is itself an object of CSS properties and their corresponding easing functions. For example, to simultaneously animate the width using the linear
easing function and the height using the easeOutBounce
easing function:
-$( "#clickme" ).click(function() {
+$( "#clickme" ).on( "click", function() {
$( "#book" ).animate({
width: "toggle",
height: "toggle"
@@ -131,7 +137,7 @@ $( "#clickme" ).click(function() {
@@ -185,7 +191,7 @@ $( "#left" ).click(function(){
The second button starts a traditional chained animation, where each animation will start once the previous animation on the element has completed.
Since .append()
can accept any number of additional arguments, the same result can be achieved by passing in the three <div>
s as three separate arguments, like so: $('body').append( $newdiv1, newdiv2, existingdiv1 )
. The type and number of arguments will largely depend on how you collect the elements in your code.
this
refers to the current element in the set.
+ this
refers to the current element in the set.
+ this
refers to the current element in the set.
The .before()
and .insertBefore()
methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .before()
, the selector expression preceding the method is the container before which the content is inserted. With .insertBefore()
, on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted before the target container.
The .before()
and .insertBefore()
methods perform the same task. The major difference is in the syntax—specifically, in the placement of the content and target. With .before()
, the content to be inserted comes from the method's argument: $(target).before(contentToBeInserted)
. With .insertBefore()
, on the other hand, the content precedes the method and is inserted before the target, which in turn is passed as the .insertBefore()
method's argument: $(contentToBeInserted).insertBefore(target)
.
Consider the following HTML:
<div class="container">
@@ -62,17 +86,12 @@ $( ".container" ).before( $( "h2" ) );
<div class="inner">Goodbye</div>
</div>
- If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.
-In jQuery 1.4, .before()
and .after()
will also work on disconnected DOM nodes:
-$( "<div>" ).before( "<p></p>" );
-
- The result is a jQuery set that contains a paragraph and a div (in that order).
+Important: If there is more than one target element, however, cloned copies of the inserted element will be created for each target except for the last one.
Similar to other content-adding methods such as .prepend()
and .after()
, .before()
also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.
For example, the following will insert two new <div>
s and an existing <div>
before the first paragraph:
-var newdiv1 = $( "<div id='object1'/>" ),
+var newdiv1 = $( "<div id='object1'></div>" ),
newdiv2 = document.createElement( "div" ),
existingdiv1 = document.getElementById( "foo" );
@@ -80,6 +99,8 @@ $( "p" ).first().before( newdiv1, [ newdiv2, existingdiv1 ] );
Since .before()
can accept any number of additional arguments, the same result can be achieved by passing in the three <div>
s as three separate arguments, like so: $( "p" ).first().before( $newdiv1, newdiv2, existingdiv1 )
. The type and number of arguments will largely depend on how you collect the elements in your code.
@@ -258,4 +259,5 @@ $( "div.test" ).bind({
This API is deprecated.
+Instead of .blur( handler )
or .blur( eventData, handler )
, use .on( "blur", handler )
or .on( "blur", eventData, handler )
, respectively.
Instead of .blur()
, use .trigger( "blur" )
.
"blur"
.This method is a shortcut for .on( "blur", handler )
in the first two variations, and .trigger( "blur" )
in the third.
This page describes the blur
event. For the deprecated .blur()
method, see .blur()
.
The blur
event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input>
. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.
For example, consider the HTML:
@@ -32,32 +32,55 @@
<div id="other">
Trigger the handler
</div>
-The event handler can be bound to the first input field:
-$( "#target" ).blur(function() {
- alert( "Handler for .blur() called." );
-});
+
+ The event handler can be bound to the first input field:
+
+$( "#target" ).on( "blur", function() {
+ alert( "Handler for `blur` called." );
+} );
Now if the first field has the focus, clicking elsewhere or tabbing away from it displays the alert:
- Handler for .blur() called. + Handler for `blur` called.
-To trigger the event programmatically, apply .blur()
without an argument:
To trigger the event programmatically, call .trigger( "blur" )
:
-$( "#other" ).click(function() {
- $( "#target" ).blur();
-});
+$( "#other" ).on( "click", function() {
+ $( "#target" ).trigger( "blur" );
+} );
After this code executes, clicks on Trigger the handler will also alert the message.
-The blur
event does not bubble in Internet Explorer. Therefore, scripts that rely on event delegation with the blur
event will not work consistently across browsers. As of version 1.4.2, however, jQuery works around this limitation by mapping blur
to the focusout
event in its event delegation methods, .live()
and .delegate()
.
The blur
event does not bubble. As of version 1.4.2, jQuery works around this limitation by mapping blur
to the focusout
event in its event delegation methods.
The native blur
event is asynchronous in all versions of IE, contrary to other browsers. To avoid issues related to this discrepancy, as of jQuery 3.7.0, jQuery uses focusout
as the native backing event for blur
in IE.
"blur"
.See the description for .on( "blur", ... )
.
"change"
.See the description for .on( "change", ... )
.