●jQuery

●jQuery UI

●jQuery Mobile

●Sizzle

●QUnit
 


●Plugins

●Contribute  

●CLA

●Style Guides

●Bug Triage

●Code

●Documentation

●Web Sites
 


●Events  



●Support  

●Learning Center

●Chat

●Stack Overflow

●Report a bug
 


●OpenJS Foundation  

●Join

●Members

●jQuery Team

●Governance

●Conduct

●Projects
 





jQuery API Documentation

 


 

●Download

●API Documentation

●Blog

●Plugins

●Browser Support

●Version Support
 




jQuery()



Categories: Core 

Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string.

Contents:



● jQuery( selector [, context ] )

●jQuery( selector [, context ] )

●jQuery( element )

●jQuery( elementArray )

●jQuery( object )

●jQuery( selection )

●jQuery()



● jQuery( html [, ownerDocument ] )

●jQuery( html [, ownerDocument ] )

●jQuery( html, attributes )



● jQuery( callback )
●jQuery( callback )



jQuery( selector [, context ] )Returns: jQuery



Description: Accepts a string containing a CSS selector which is then used to match a set of elements.


version added: 1.0jQuery( selector [, context ] )




selector

Type: Selector

A string containing a selector expression



context

Type: ElementorjQueryorSelector

A DOM Element, Document, jQuery or selector to use as context




version added: 1.0jQuery( element )



element

Type: Element

A DOM element to wrap in a jQuery object.



version added: 1.0jQuery( elementArray )



elementArray

Type: Array

An array containing a set of DOM elements to wrap in a jQuery object.



version added: 1.0jQuery( object )



object

Type: PlainObject

A plain object to wrap in a jQuery object.



version added: 1.0jQuery( selection )



selection

Type: jQuery

An existing jQuery object to clone.



version added: 1.4jQuery()


This signature does not accept any arguments.




In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:

1
$( "div.foo" );


If no elements match the provided selector, the new jQuery object is "empty"; that is, it contains no elements and has .length property of 0.

Selector Context


By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, to do a search within an event handler, the search can be restricted like so:

1
2
3
$( "div.foo" ).on( "click", function() {
$( "span", this ).addClass( "bar" );
});


When the search for the span selector is restricted to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $( "span", this ) is equivalent to $( this ).find( "span" ).

Using DOM elements


The second and third formulations of this function create a jQuery object using one or more DOM elements that were already selected in some other way. A jQuery object is created from the array elements in the order they appeared in the array; unlike most other multi-element jQuery operations, the elements are not sorted in DOM order. Elements will be copied from the array as-is and won't be unwrapped if they're already jQuery collections.

Please note that although you can pass text nodes and comment nodes into a jQuery collection this way, most operations don't support them. The few that do will have an explicit note on their API documentation page.

A common use of single-DOM-element construction is to call jQuery methods on an element that has been passed to a callback function through the keyword this:

1
2
3
$( "div.foo" ).on( "click", function() {
$( this ).slideUp();
});


This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked item in the this keyword as a bare DOM element, the element must be passed to the $() function before applying jQuery methods to it.

XML data returned from an Ajax call can be passed to the $() function so individual elements of the XML structure can be retrieved using .find() and other DOM traversal methods.

1
2
3
$.post( "url.xml", function( data ) {
var $child = $( data ).find( "child" );
});

Cloning jQuery Objects


When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

Returning an Empty Set


Calling the jQuery() method with no arguments returns an empty jQuery set (with a .length property of 0). Similarly, if an argument of null, undefined, an empty array ([]), or an empty string ("") is passed, the set contains no elements.

Working With Plain Objects


At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: .data(),.prop(),.on(), .off(), .trigger() and .triggerHandler(). The use of .data() (or any method requiring .data()) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Define a plain object
var foo = { foo: "bar", hello: "world" };
// Pass it to the jQuery function
var $foo = $( foo );
// Test accessing property values
var test1 = $foo.prop( "foo" ); // bar
// Test setting property values
$foo.prop( "foo", "foobar" );
var test2 = $foo.prop( "foo" ); // foobar
// Test using .data() as summarized above
$foo.data( "keyName", "someValue" );
console.log( $foo ); // will now contain a jQuery{randomNumber} property
// Test binding an event name and triggering
$foo.on( "eventName", function () {
console.log( "eventName was called" );
});
$foo.trigger( "eventName" ); // Logs "eventName was called"


Should .trigger( "eventName" ) be used, it will search for an "eventName" property on the object and attempt to execute it after any attached jQuery handlers are executed. It does not check whether the property is a function or not. To avoid this behavior, .triggerHandler( "eventName" ) should be used instead.

1
$foo.triggerHandler( "eventName" ); // Also logs "eventName was called"


Examples:


 Example 1


Find all p elements that are children of a div element and apply a border to them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery demo</title>
<script src="https://code.jquery.com/jquery-4.0.0.js"></script>
</head>
<body>
<p>one</p>
<div><p>two</p></div>
<p>three</p>
<script>
$( "div > p" ).css( "border", "1px solid gray" );
</script>
</body>
</html>

Demo:




 Example 2


Find all inputs of type radio within the first form in the document.

1
$( "input:radio", document.forms[ 0 ] );



Example 3


Find all div elements within an XML document from an Ajax response.

1
$( "div", xml.responseXML );



Example 4


Set the background color of the page to black.

1
$( document.body ).css( "background", "black" );



Example 5


Hide all the input elements within a form.

1
$( myForm.elements ).hide();


jQuery( html [, ownerDocument ] )Returns: jQuery



Description: Creates DOM elements on the fly from the provided string of raw HTML.


version added: 1.0jQuery( html [, ownerDocument ] )




html

Type: htmlString

A string of HTML to create on the fly. Note that this parses HTML, not XML.



ownerDocument

Type: Document

A document in which the new elements will be created.




version added: 1.4jQuery( html, attributes )




html

Type: htmlString

A string defining a single, standalone, HTML element (e.g. <div/> or <div></div>).



attributes

Type: PlainObject

An object of attributes, events, and methods to call on the newly-created element.





Creating New Elements


If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it starts with <tag ... >). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. You can perform any of the usual jQuery methods on this object:

1
$( "<p id='test'>My <em>new</em> text</p>" ).appendTo( "body" );


For explicit parsing of a string to HTML, use the $.parseHTML() method.

By default, elements are created with an .ownerDocument matching the document into which the jQuery library was loaded. Elements being injected into a different document should be created using that document, e.g., $("<p>hello iframe</p>", $("#myiframe").prop("contentWindow").document).

If the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's .innerHTML mechanism. In most cases, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag (with optional closing tag or quick-closing) — $( "<img />" )or$( "<img>" ), $( "<a></a>" )or$( "<a>" ) — jQuery creates the element using the native JavaScript .createElement() function.

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed.

Filtering isn't, however, limited to these tags. For example, Internet Explorer prior to version 8 will also convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.

To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:

1
$( "<a href='https://jquery.com'></a>" );


Tags that cannot contain elements may be quick-closed or not:

1
2
$( "<img />" );
$( "<input>" );


When passing HTML to jQuery(), note that text nodes are not treated as DOM elements. With the exception of a few methods (such as .content()), they are generally ignored or removed. E.g:

1
2
var el = $( "<br>2<br>3" ); // returns [<br>, "2", <br>]
el = $( "<br>2<br>3 >" ); // returns [<br>, "2", <br>, "3 &gt;"]


This behavior is expected. As of jQuery 1.9.0 (and unless using the jQuery Migrate plugin), jQuery() requires the HTML string to start with a < (i.e text nodes cannot appear at the front of the HTML string).

As of jQuery 1.4, the second argument to jQuery() can accept a plain object consisting of a superset of the properties that can be passed to the .attr() method.

Important: If the second argument is passed, the HTML string in the first argument must represent a simple element with no attributes. As of jQuery 1.4, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.

As of jQuery 1.8, any jQuery instance method (a method of jQuery.fn) can be used as a property of the object passed to the second parameter:

1
2
3
4
5
6
7
8
$( "<div></div>", {
"class": "my-div",
on: {
touchstart: function( event ) {
// Do something
}
}
}).appendTo( "body" );


The name "class" must be quoted in the object since it is a JavaScript reserved word, and "className" cannot be used since it refers to the DOM property, not the attribute. 

While the second argument is convenient, its flexibility can lead to unintended consequences (e.g. $( "<input>", {size: "4"} ) calling the .size() method instead of setting the size attribute). The previous code block could thus be written instead as:

1
2
3
4
5
6
7
8
$( "<div></div>" )
.addClass( "my-div" )
.on({
touchstart: function( event ) {
// Do something
}
})
.appendTo( "body" );


Examples:


 Example 1


Create a div element (and all of its contents) dynamically and append it to the body element. Internally, an element is created and its innerHTML property set to the given markup.

1
$( "<div><p>Hello</p></div>" ).appendTo( "body" )



Example 2


Create some DOM elements.

1
2
3
4
5
6
7
8
$( "<div/>", {
"class": "test",
text: "Click me!",
click: function() {
$( this ).toggleClass( "test" );
}
})
.appendTo( "body" );


jQuery( callback )Returns: jQuery



Description: Binds a function to be executed when the DOM has finished loading.

version added: 1.0jQuery( callback )



callback

Type: Function()

The function to execute when the DOM is ready.




This function behaves just like $( document ).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready. While this function is, technically, chainable, there really isn't much use for chaining against it.
 

Examples:


 Example 1


Execute the function when the DOM is ready to be used.

1
2
3
$(function() {
// Document is ready
});



Example 2


Use both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

1
2
3
jQuery(function( $ ) {
// Your code using failsafe $ alias here...
});





●Ajax

●Global Ajax Event Handlers

●Helper Functions

●Low-Level Interface

●Shorthand Methods



●Attributes

●Callbacks Object

●Core

●CSS

●Data

●Deferred Object

●Deprecated

●Deprecated 1.3

●Deprecated 1.7

●Deprecated 1.8

●Deprecated 1.9

●Deprecated 1.10 & 2.0

●Deprecated 3.0

●Deprecated 3.2

●Deprecated 3.3

●Deprecated 3.4

●Deprecated 3.5

●Deprecated 3.7



●Dimensions

●Effects

●Basics

●Custom

●Fading

●Sliding



●Events

●Browser Events

●Document Loading

●Event Handler Attachment

●Event Object

●Form Events

●Keyboard Events

●Mouse Events



●Forms

●Internals

●Manipulation

●Class Attribute

●Copying

●DOM Insertion, Around

●DOM Insertion, Inside

●DOM Insertion, Outside

●DOM Removal

●DOM Replacement

●General Attributes

●Style Properties



●Miscellaneous

●Collection Manipulation

●Data Storage

●DOM Element Methods

●Setup Methods



●Offset

●Properties

●Properties of jQuery Object Instances

●Properties of the Global jQuery Object



●Removed

●Selectors

●Attribute

●Basic

●Basic Filter

●Child Filter

●Content Filter

●Form

●Hierarchy

●jQuery Extensions

●Visibility Filter



●Traversing

●Filtering

●Miscellaneous Traversing

●Tree Traversal



●Utilities

●Version

●Version 1.0

●Version 1.0.4

●Version 1.1

●Version 1.1.2

●Version 1.1.3

●Version 1.1.4

●Version 1.2

●Version 1.2.3

●Version 1.2.6

●Version 1.3

●Version 1.4

●Version 1.4.1

●Version 1.4.2

●Version 1.4.3

●Version 1.4.4

●Version 1.5

●Version 1.5.1

●Version 1.6

●Version 1.7

●Version 1.8

●Version 1.9

●Version 1.11 & 2.1

●Version 1.12 & 2.2

●Version 3.0

●Version 3.1

●Version 3.2

●Version 3.3

●Version 3.4

●Version 3.5

●Version 3.6

●Version 3.7

●Version 4.0


 






Books



Learning jQuery 4th Edition by Karl Swedberg and Jonathan Chaffer Learning jQuery Fourth Edition  Karl Swedberg and Jonathan Chaffer  

jQuery in Action by Bear Bibeault, Yehuda Katz, and Aurelio De Rosa jQuery in Action  Bear Bibeault, Yehuda Katz, and Aurelio De Rosa  

jQuery Succinctly by Cody Lindley jQuery Succinctly  Cody Lindley  






●Learning Center

●Chat

●Twitter

●GitHub
 

Copyright 2026 OpenJS Foundation and jQuery contributors. All rights reserved. See jQuery License for more information. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. OpenJS Foundation Terms of Use, Privacy, and Cookie Policies also apply.  

Web hosting by Digital Ocean | CDN by Fastly | Powered by WordPress