-
add( String, DOMElement, Array<DOMElement> expr ) returns jQuery
Adds more elements, matched by the given expression, to the set of matched elements.
Example:
Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.
$("div").css("border", "2px solid red") .add("p") .css("background", "yellow");HTML:
<div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <p>Added this... (notice no border)</p>Example:
Adds more elements, matched by the given expression, to the set of matched elements.
$("p").add("span").css("background", "yellow");HTML:
<p>Hello</p><span>Hello Again</span>Example:
Adds more elements, created on the fly, to the set of matched elements.
$("p").clone().add("<span>Again</span>").appendTo(document.body);HTML:
<p>Hello</p>Example:
Adds one or more Elements to the set of matched elements.
$("p").add(document.getElementById("a")).css("background", "yellow");HTML:
<p>Hello</p><span id="a">Hello Again</span> -
addClass( String class ) returns jQuery
Adds the specified class(es) to each of the set of matched elements.
Example:
Adds the class 'selected' to the matched elements.
$("p:last").addClass("selected");HTML:
<p>Hello</p> <p>and</p> <p>Goodbye</p>Example:
Adds the classes 'selected' and 'highlight' to the matched elements.
$("p:last").addClass("selected highlight");HTML:
<p>Hello</p> <p>and</p> <p>Goodbye</p> -
after( String, Element, jQuery content ) returns jQuery
Insert content after each of the matched elements.
Example:
Inserts some HTML after all paragraphs.
$("p").after("<b>Hello</b>");HTML:
<p>I would like to say: </p>Example:
Inserts a DOM element after all paragraphs.
$("p").after( document.createTextNode("Hello") );HTML:
<p>I would like to say: </p>Example:
Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.
$("p").after( $("b") );HTML:
<b>Hello</b> <p>I would like to say: </p> -
ajaxComplete( Function callback ) returns jQuery
Attach a function to be executed whenever an AJAX request completes. This is an <a href='Ajax_Events'>Ajax Event</a>.
The XMLHttpRequest and settings used for that request are passed as arguments to the callback.Example:
Show a message when an AJAX request completes.
$("#msg").ajaxComplete(function(request, settings){ $(this).append("<li>Request Complete.</li>"); }); -
ajaxError( Function callback ) returns jQuery
Attach a function to be executed whenever an AJAX request fails. This is an <a href='Ajax_Events'>Ajax Event</a>.
The XMLHttpRequest and settings used for that request are passed as arguments to the callback. A third argument, an exception object, is passed if an exception occured while processing the request.Example:
Show a message when an AJAX request fails.
$("#msg").ajaxError(function(request, settings){ $(this).append("<li>Error requesting page " + settings.url + "</li>"); }); -
ajaxSend( Function callback ) returns jQuery
Attach a function to be executed before an AJAX request is sent. This is an <a href='Ajax_Events'>Ajax Event</a>.
The XMLHttpRequest and settings used for that request are passed as arguments to the callback.Example:
Show a message before an AJAX request is sent.
$("#msg").ajaxSend(function(evt, request, settings){ $(this).append("<li>Starting request at " + settings.url + "</li>"); }); -
ajaxStart( Function callback ) returns jQuery
Attach a function to be executed whenever an AJAX request begins and there is none already active. This is an <a href='Ajax_Events'>Ajax Event</a>.
Example:
Show a loading message whenever an AJAX request starts (and none is already active).
$("#loading").ajaxStart(function(){ $(this).show(); }); -
ajaxStop( Function callback ) returns jQuery
Attach a function to be executed whenever all AJAX requests have ended. This is an <a href='Ajax_Events'>Ajax Event</a>.
Example:
Hide a loading message after all the AJAX requests have stopped.
$("#loading").ajaxStop(function(){ $(this).hide(); }); -
ajaxSuccess( Function callback ) returns jQuery
Attach a function to be executed whenever an AJAX request completes successfully. This is an <a href='Ajax_Events'>Ajax Event</a>.
The event object, XMLHttpRequest, and settings used for that request are passed as arguments to the callback.Example:
Show a message when an AJAX request completes successfully.
$("#msg").ajaxSuccess(function(evt, request, settings){ $(this).append("<li>Successful Request!</li>"); }); -
all( ) returns Array<Element>
Matches all elements.
Most useful when combined with a context to search in.Example:
Finds every element (including head, body, etc).
$("*").css("border","3px solid red");HTML:
<div>DIV</div> <span>SPAN</span> <p>P <button>Button</button></p>Example:
A common way to find all elements is to set the 'context' to document.body so elements like head, script, etc are left out.
$("*", document.body).css("border","3px solid red"); -
andSelf( ) returns jQuery
Add the previous selection to the current selection.
Useful for traversing elements, and then adding something that was matched before the last traversion.Example:
Find all divs, and all the paragraphs inside of them, and give them both classnames. Notice the div doesn't have the yellow background color since it didn't use andSelf().
$("div").find("p").andSelf().addClass("border"); $("div").find("p").addClass("background");HTML:
<div> <p>First Paragraph</p> <p>Second Paragraph</p> </div> -
animate( Options params, Options options ) returns jQuery
A function for making your own, custom animations.
The key aspect of this function is the object of style properties that will be animated, and to what end. Each key within the object represents a style property that will also be animated (for example: "height", "top", or "opacity").
Note that properties should be specified using camel case eg. marginLeft instead of margin-left.
The value associated with the key represents to what end the property will be animated. If a number is provided as the value, then the style property will be transitioned from its current state to that new number. Otherwise if the string "hide", "show", or "toggle" is provided, a default animation will be constructed for that property.Options
Example:
The first button shows how an unqueued animation works. It expands the div out to 90% width '''while''' the font-size is increasing. Once the font-size change is complete, the border animation will begin. The second button starts a traditional chained animation, where each animation will start once the previous animation on the element has completed.
$("#go1").click(function(){ $("#block1").animate( { width:"90%" }, { queue:false, duration:3000 } ) .animate( { fontSize:"24px" }, 1500 ) .animate( { borderRightWidth:"15px" }, 1500); }); $("#go2").click(function(){ $("#block2").animate( { width:"90%"}, 1000 ) .animate( { fontSize:"24px" } , 1000 ) .animate( { borderLeftWidth:"15px" }, 1000); }); $("#go3").click(function(){ $("#go1").add("#go2").click(); }); $("#go4").click(function(){ $("div").css({width:"", fontSize:"", borderWidth:""}); });HTML:
<button id="go1">» Animate Block1</button> <button id="go2">» Animate Block2</button> <button id="go3">» Animate Both</button> <button id="go4">» Reset</button> <div id="block1">Block1</div> <div id="block2">Block2</div>Example:
Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds.
$("p").animate({ "height": "toggle", "opacity": "toggle" }, { duration: "slow" });Example:
Animates all paragraph to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds. It also will do it ''outside'' the queue, meaning it will automatically start without waiting for its turn.
$("p").animate({ left: "50px", opacity: 1 }, { duration: 500, queue: false });Example:
An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function.
$("p").animate({ "opacity": "show" }, { "duration": "slow", "easing": "easein" }); -
animate( Options params, String, Number duration, String easing, Function callback ) returns jQuery
A function for making your own, custom animations.
The key aspect of this function is the object of style properties that will be animated, and to what end. Each key within the object represents a style property that will also be animated (for example: "height", "top", or "opacity").
Note that properties should be specified using camel case eg. marginLeft instead of margin-left.
The value associated with the key represents to what end the property will be animated. If a number is provided as the value, then the style property will be transitioned from its current state to that new number. Otherwise if the string "hide", "show", or "toggle" is provided, a default animation will be constructed for that property. Only properties that take numeric values are supported (e.g. backgroundColor is not supported by animate()).
As of jQuery 1.2 you can now animate properties by em and % (where applicable). Additionally, in jQuery 1.2, you can now do relative animations - specifying a "''+=''" or "''-=''" in front of the property value to move the element positively, or negatively, relative to the current position.Example:
Click the button to animate the div with a number of different properties.
// Using multiple unit types within one animation. $("#go").click(function(){ $("#block").animate({ width: "70%", opacity: 0.4, marginLeft: "0.6in", fontSize: "3em", borderWidth: "10px" }, 1500 ); });HTML:
<button id="go">» Run</button> <div id="block">Hello!</div>Example:
Shows a div animate with a relative move. Click several times on the buttons to see the relative animations queued up.
$("#right").click(function(){ $(".block").animate({"left": "+=50px"}, "slow"); }); $("#left").click(function(){ $(".block").animate({"left": "-=50px"}, "slow"); });HTML:
<button id="left">«</button> <button id="right">»</button> <div class="block"></div>Example:
Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds.
$("p").animate({ "height": "toggle", "opacity": "toggle" }, "slow");Example:
Animates all paragraph to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds.
$("p").animate({ "left": "50", "opacity": 1 }, 500);Example:
An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function. Note, this code will do nothing unless the paragraph element is hidden.
$("p").animate({ "opacity": "show" }, "slow", "easein"); -
animated( ) returns Array<Element>
Matches all elements that are currently being animated.
Example:
Change the color of any div that is animated.
$("#run").click(function(){ $("div:animated").toggleClass("colored"); }); function animateIt() { $("#mover").slideToggle("slow", animateIt); } animateIt();HTML:
<button id="run">Run</button> <div></div> <div id="mover"></div> <div></div> -
append( String, Element, jQuery content ) returns jQuery
Append content to the inside of every matched element.
This operation is similar to doing an appendChild to all the specified elements, adding them into the document.Example:
Appends some HTML to all paragraphs.
$("p").append("<b>Hello</b>");HTML:
<p>I would like to say: </p>Example:
Appends an Element to all paragraphs.
$("p").append(document.createTextNode("Hello"));HTML:
<p>I would like to say: </p>Example:
Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
$("p").append( $("b") );HTML:
<b>Hello</b><p>I would like to say: </p> -
appendTo( String content ) returns jQuery
Append all of the matched elements to another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular $(A).append(B), in that instead of appending B to A, you're appending A to B.Example:
Appends all spans to the element with the ID "foo"
$("span").appendTo("#foo"); // check append() examplesHTML:
<span>I have nothing more to say... </span> <div id="foo">FOO! </div> -
attr( String name ) returns Object
Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned.
Example:
Finds the title attribute of the first <em> in the page.
var title = $("em").attr("title"); $("div").text(title);HTML:
<p> Once there was a <em title="huge, gigantic">large</em> dinosaur... </p> The title of the emphasis is:<div></div> -
attr( Map properties ) returns jQuery
Set a key/value object as properties to all matched elements.
This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ).Example:
Set some attributes for all <img>s in the page.
$("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" }); $("div").text($("img").attr("alt"));HTML:
<img /> <img /> <img /> <div></div> -
attr( String key, Object value ) returns jQuery
Set a single property to a value, on all matched elements.
Example:
Disables buttons greater than the 0th button.
$("button:gt(0)").attr("disabled","disabled");HTML:
<button>0th Button</button> <button>1st Button</button> <button>2nd Button</button> -
attr( String key, Function fn ) returns jQuery
Set a single property to a computed value, on all matched elements.
Instead of supplying a string value as described <a href='#keyvalue'>above</a>, a function is provided that computes the value.Example:
Sets id for divs based on the position in the page.
$("div").attr("id", function (arr) { return "div-id" + arr[0]; }) .each(function () { $("span", this).html("(ID = '<b>" + this.id + "</b>')"); });HTML:
<div>Zero-th <span></span></div> <div>First <span></span></div> <div>Second <span></span></div>Example:
Sets src attribute from title attribute on the image.
$("img").attr("src", function() { return "/images/" + this.title; });HTML:
<img title="hat.gif"/> <img title="hat-old.gif"/> <img title="hat2-old.gif"/> -
attributeContains( String attribute, String value ) returns Array<Element>
Matches elements that have the specified attribute and it contains a certain value.
Example:
Finds all inputs that with a name attribute that contains 'man' and sets the value with some text.
$("input[name*='man']").val("has man in it!");HTML:
<input name="man-news" /> <input name="milkman" /> <input name="letterman2" /> <input name="newmilk" /> -
attributeEndsWith( String attribute, String value ) returns Array<Element>
Matches elements that have the specified attribute and it ends with a certain value.
Example:
Finds all inputs with an attribute name that ends with 'letter' and puts text in them.
$("input[name$='letter']").val("a letter");HTML:
<input name="newsletter" /> <input name="milkman" /> <input name="jobletter" /> -
attributeEquals( String attribute, String value ) returns Array<Element>
Matches elements that have the specified attribute with a certain value.
Example:
Finds all inputs with name 'newsletter' and changes the text of the span next to it.
$("input[name='newsletter']").next().text(" is newsletter");HTML:
<div> <input type="radio" name="newsletter" value="Hot Fuzz" /> <span>name?</span> </div> <div> <input type="radio" name="newsletter" value="Cold Fusion" /> <span>name?</span> </div> <div> <input type="radio" name="accept" value="Evil Plans" /> <span>name?</span> </div> -
attributeHas( String attribute ) returns Array<Element>
Matches elements that have the specified attribute.
Example:
Bind a single click that adds the div id to its text.
$("div[id]").one("click", function(){ var idString = $(this).text() + " = " + $(this).attr("id"); $(this).text(idString); });HTML:
<div>no id</div> <div id="hey">with id</div> <div id="there">has an id</div> <div>nope</div> -
attributeMultiple( Selector selector1, Selector selector2, Selector selectorN ) returns Array<Element>
Matches elements that have the specified attribute and it contains a certain value.
Example:
Finds all inputs that have an id attribute and whose name attribute ends with man and sets the value.
$("input[id][name$='man']").val("only this one");HTML:
<input id="man-news" name="man-news" /> <input name="milkman" /> <input id="letterman" name="new-letterman" /> <input name="newmilk" /> -
attributeNotEqual( String attribute, String value ) returns Array<Element>
Matches elements that don't have the specified attribute with a certain value.
Example:
Finds all inputs that don't have the name 'newsletter' and changes the text of the span next to it.
$("input[name!='newsletter']").next().text(" is not newsletter");HTML:
<div> <input type="radio" name="newsletter" value="Hot Fuzz" /> <span>name?</span> </div> <div> <input type="radio" name="newsletter" value="Cold Fusion" /> <span>name?</span> </div> <div> <input type="radio" name="accept" value="Evil Plans" /> <span>name?</span> </div> -
attributeStartsWith( String attribute, String value ) returns Array<Element>
Matches elements that have the specified attribute and it starts with a certain value.
Example:
Finds all inputs with an attribute name that starts with 'news' and puts text in them.
$("input[name^='news']").val("news here!");HTML:
<input name="newsletter" /> <input name="milkman" /> <input name="newsboy" /> -
before( String, Element, jQuery content ) returns jQuery
Insert content before each of the matched elements.
Example:
Inserts some HTML before all paragraphs.
$("p").before("<b>Hello</b>");HTML:
<p> is what I said...</p>Example:
Inserts a DOM element before all paragraphs.
$("p").before( document.createTextNode("Hello") );HTML:
<p> is what I said...</p>Example:
Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.
$("p").before( $("b") );HTML:
<p> is what I said...</p><b>Hello</b> -
bind( String type, Object data, Function fn ) returns jQuery
Binds a handler to a particular event (like click) for each matched element. Can also bind custom events.
The event handler is passed an event object that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler has to return false. Note that this will prevent handlers on parent elements from running but not other jQuery handlers on the same element.
In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second parameter (and the handler function as the third), see second example.Example:
Handle click and double-click for the paragraph. Note: the coordinates are window relative so in this case relative to the demo iframe.
$("p").bind("click", function(e){ var str = "( " + e.pageX + ", " + e.pageY + " )"; $("span").text("Click happened! " + str); }); $("p").bind("dblclick", function(){ $("span").text("Double-click happened in " + this.tagName); });HTML:
<p>Click or double click here.</p> <span></span>Example:
To display each paragraph's text in an alert box whenever it is clicked:
$("p").bind("click", function(){ alert( $(this).text() ); });Example:
You can pass some extra data before the event handler:
function handler(event) { alert(event.data.foo); } $("p").bind("click", {foo: "bar"}, handler)Example:
To cancel a default action and prevent it from bubbling up, return false:
$("form").bind("submit", function() { return false; })Example:
To cancel only the default action by using the preventDefault method.
$("form").bind("submit", function(event){ event.preventDefault(); });Example:
Stop only an event from bubbling by using the stopPropagation method.
$("form").bind("submit", function(event){ event.stopPropagation(); });Example:
Can bind custom events too.
$("p").bind("myCustomEvent", function(e, myName, myValue){ $(this).text(myName + ", hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent", [ "John" ]); });HTML:
<p>Has an attached custom event.</p> <button>Trigger custom event</button> <span style="display:none;"></span> -
blur( ) returns jQuery
Triggers the blur event of each matched element.
This causes all of the functions that have been bound to that blur event to be executed, and calls the browser's default blur action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the blur event. The blur event usually fires when an element loses focus either via the pointing device or by tabbing navigationExample:
To triggers the blur event on all paragraphs:
$("p").blur(); -
blur( Function fn ) returns jQuery
Bind a function to the blur event of each matched element.
The blur event fires when an element loses focus either via the pointing device or by tabbing navigation.Example:
To pop up an alert saying "Hello World!" every time any paragraph's blur event triggers:
$("p").blur( function () { alert("Hello World!"); } ); -
change( ) returns jQuery
Triggers the change event of each matched element.
This causes all of the functions that have been bound to that change event to be executed, and calls the browser's default change action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the change event. The change event usually fires when a control loses the input focus and its value has been modified since gaining focus. -
change( Function fn ) returns jQuery
Binds a function to the change event of each matched element.
The change event fires when a control loses the input focus and its value has been modified since gaining focus.Example:
Attaches a change even to the select that gets the text for each selected option and writes them in the div. It then triggers the event for the initial text draw.
$("select").change(function () { var str = ""; $("select option:selected").each(function () { str += $(this).text() + " "; }); $("div").text(str); }) .change();HTML:
<select name="sweets" multiple="multiple"> <option>Chocolate</option> <option selected="selected">Candy</option> <option>Taffy</option> <option selected="selected">Carmel</option> <option>Fudge</option> <option>Cookie</option> </select> <div></div>Example:
To add a validity test to all text input elements:
$("input[@type='text']").change( function() { // check input for validity here }); -
checkbox( ) returns Array<Element>
Matches all input elements of type checkbox.
Example:
Finds all checkbox inputs.
var input = $(":checkbox").css({background:"yellow", border:"3px red solid"}); $("div").text("For this type jQuery found " + input.length + ".") .css("color", "red"); $("form").submit(function () { return false; }); // so it won't submitHTML:
<form> <input type="button" value="Input Button"/> <input type="checkbox" /> <input type="checkbox" /> <input type="file" /> <input type="hidden" /> <input type="image" /> <input type="password" /> <input type="radio" /> <input type="reset" /> <input type="submit" /> <input type="text" /> <select><option>Option<option/></select> <textarea></textarea> <button>Button</button> </form> <div> </div> -
checked( ) returns Array<Element>
Matches all elements that are checked.
Example:
Finds all input elements that are checked.
function countChecked() { var n = $("input:checked").length; $("div").text(n + (n == 1 ? " is" : " are") + " checked!"); } countChecked(); $(":checkbox").click(countChecked);HTML:
<form> <input type="checkbox" name="newsletter" checked="checked" value="Hourly" /> <input type="checkbox" name="newsletter" value="Daily" /> <input type="checkbox" name="newsletter" value="Weekly" /> <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> <input type="checkbox" name="newsletter" value="Yearly" /> </form> <div></div>Result:
[ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ] -
child( Selector parent, Selector child ) returns Array<Element>
Matches all child elements specified by "child" of elements specified by "parent".
Example:
Finds all children of the element with id "main" which is yellow.
$("#main > *").css("border", "3px double red");HTML:
<span id="main"> <div></div> <button>Child</button> <div class="mini"></div> <div> <div class="mini"></div> <div class="mini"></div> </div> <div><button>Grand</button></div> <div><span>A Span <em>in</em> child</span></div> <span>A Span in main</span> </span> -
children( String expr ) returns jQuery
Get a set of elements containing all of the unique immediate children of each of the matched set of elements.
This set can be filtered with an optional expression that will cause only elements matching the selector to be collected. Also note: while parents() will look at all ancestors, children() will only consider immediate child elements.Example:
Find all children of the clicked element.
$("#container").click(function (e) { $("*").removeClass("hilite"); var $kids = $(e.target).children(); var len = $kids.addClass("hilite").length; $("#results span:first").text(len); $("#results span:last").text(e.target.tagName); e.preventDefault(); return false; });HTML:
<div id="container"> <div> <p>This <span>is the <em>way</em> we</span> write <em>the</em> demo,</p> </div> <div> <a href="#"><b>w</b>rit<b>e</b></a> the <span>demo,</span> <button>write the</button> demo, </div> <div> This <span>the way we <em>write</em> the <em>demo</em> so</span> <input type="text" value="early" /> in </div> <p> <span>t</span>he <span>m</span>orning. <span id="results">Found <span>0</span> children in <span>TAG</span>.</span> </p> </div>Example:
Find all children of each div.
$("div").children().css("border-bottom", "3px double red");HTML:
<p>Hello (this is a paragraph)</p> <div><span>Hello Again (this span is a child of the a div)</span></div> <p>And <span>Again</span> (in another paragraph)</p> <div>And One Last <span>Time</span> (most text directly in a div)</div>Example:
Find all children with a class "selected" of each div.
$("div").children(".selected").css("color", "blue");HTML:
<div> <span>Hello</span> <p class="selected">Hello Again</p> <div class="selected">And Again</div> <p>And One Last Time</p> </div> -
class( String class ) returns Array<Element>
Matches all elements with the given class.
Example:
Finds the element with the class "myClass".
$(".myClass").css("border","3px solid red");HTML:
<div class="notMe">div class="notMe"</div> <div class="myClass">div class="myClass"</div> <span class="myClass">span class="myClass"</span> -
click( ) returns jQuery
Triggers the click event of each matched element.
Causes all of the functions that have been bound to that click event to be executed.Example:
To trigger the click event on all of the paragraphs on the page:
$("p").click(); -
click( Function fn ) returns jQuery
Binds a function to the click event of each matched element.
The click event fires when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is:<ul><li>mousedown</li><li>mouseup</li><li>click</li></ul>Example:
To hide paragraphs on a page when they are clicked:
$("p").click(function () { $(this).slideUp(); }); $("p").hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); });HTML:
<p>First Paragraph</p> <p>Second Paragraph</p> <p>Yet one more Paragraph</p> -
clone( ) returns jQuery
Clone matched DOM Elements and select the clones.
This is useful for moving copies of the elements to another location in the DOM.Example:
Clones all b elements (and selects the clones) and prepends them to all paragraphs.
$("b").clone().prependTo("p");HTML:
<b>Hello</b><p>, how are you?</p>Result:
<b>Hello</b><p><b>Hello</b>, how are you?</p> -
clone( Boolean true ) returns jQuery
Clone matched DOM Elements, and all their event handlers, and select the clones.
This is useful for moving copies of the elements, and their events, to another location in the DOM.Example:
Create a button that's able to clone itself - and have the clones themselves be clonable.
$("button").click(function(){ $(this).clone(true).insertAfter(this); });HTML:
<button>Clone Me!</button> -
contains( String text ) returns Array<Element>
Matches elements which contain the given text.
Example:
Finds all divs containing "John" and underlines them.
$("div:contains('John')").css("text-decoration", "underline");HTML:
<div>John Resig</div> <div>George Martin</div> <div>Malcom John Sinclair</div> <div>J. Ohn -
contents( ) returns jQuery
Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.
Example:
Find all the text nodes inside a paragraph and wrap them with a bold tag.
$("p").contents().not("[nodeType=1]").wrap("<b/>");HTML:
<p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p>Example:
Append some new content into an empty iframe.
$("iframe").contents().find("body").append("I'm in an iframe!");HTML:
<iframe src="/index-blank.html" width="300" height="100"></iframe> -
css( String name ) returns String
Return a style property on the first matched element.
Example:
To access the background color of a clicked div.
$("div").click(function () { var color = $(this).css("background-color"); $("#result").html("That div is <span style='color:" + color + ";'>" + color + "</span>."); });HTML:
<span id="result"> </span> <div style="background-color:blue;"></div> <div style="background-color:rgb(15,99,30);"></div> <div style="background-color:#123456;"></div> <div style="background-color:#f11;"></div> -
css( Map properties ) returns jQuery
Set a key/value object as style properties to all matched elements.
This is the best way to set several style properties on all matched elements.Example:
To set the color of all paragraphs to red and background to blue:
$("p").hover(function () { $(this).css({ backgroundColor:"yellow", fontWeight:"bolder" }); }, function () { var cssObj = { backgroundColor: "#ddd", fontWeight: "", color: "rgb(0,40,244)" } $(this).css(cssObj); });HTML:
<p> Move the mouse over a paragraph. </p> <p> Like this one or the one above. </p>Example:
If the property name includes a "-", put it between quotation marks:
$("p").hover(function () { $(this).css({ "background-color":"yellow", "font-weight":"bolder" }); }, function () { var cssObj = { "background-color": "#ddd", "font-weight": "", color: "rgb(0,40,244)" } $(this).css(cssObj); });HTML:
<p> Move the mouse over a paragraph. </p> <p> Like this one or the one above. </p> -
css( String name, String or Number value ) returns jQuery
Set a single style property to a value on all matched elements.
If a number is provided, it is automatically converted into a pixel value.Example:
To change the color of any paragraph to red on mouseover event.
$("p").mouseover(function () { $(this).css("color","red"); });HTML:
<p> Just roll the mouse over me. </p> <p> Or me to see a color change. </p>Example:
To highlight a clicked word in the paragraph.
var words = $("p:first").text().split(" "); var text = words.join("</span> <span>"); $("p:first").html("<span>" + text + "</span>"); $("span").click(function () { $(this).css("background-color","yellow"); });HTML:
<p> Once upon a time there was a man who lived in a pizza parlor. This man just loved pizza and ate it all the time. He went on to be the happiest man in the world. The end. </p> -
dblclick( ) returns jQuery
Triggers the dblclick event of each matched element.
This causes all of the functions that have been bound to that dblclick event to be executed, and calls the browser's default dblclick action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the dblclick event. The dblclick event usually fires when the pointing device button is double clicked over an element. -
dblclick( Function fn ) returns jQuery
Binds a function to the dblclick event of each matched element.
The dblclick event fires when the pointing device button is double clicked over an elementExample:
To bind a "Hello World!" alert box the dblclick event on every paragraph on the page:
$("p").dblclick( function () { alert("Hello World!"); }); -
dequeue( ) returns jQuery
Removes a queued function from the front of the queue and executes it.
Example:
Use dequeue to end a custom queue function which allows the queue to keep going.
$("button").click(function () { $("div").animate({left:'+=200px'}, 2000); $("div").animate({top:'0px'}, 600); $("div").queue(function () { $(this).toggleClass("red"); $(this).dequeue(); }); $("div").animate({left:'10px', top:'30px'}, 700); });HTML:
<button>Start</button> <div></div> -
descendant( Selector ancestor, Selector descendant ) returns Array<Element>
Matches all descendant elements specified by "descendant" of elements specified by "ancestor".
Example:
Finds all input descendants of forms.
$("form input").css("border", "2px dotted blue");HTML:
<form> <div>Form is surrounded by the green outline</div> <label>Child:</label> <input name="name" /> <fieldset> <label>Grandchild:</label> <input name="newsletter" /> </fieldset> </form> Sibling to form: <input name="none" /> -
disabled( ) returns Array<Element>
Matches all elements that are disabled.
Example:
Finds all input elements that are disabled.
$("input:disabled").val("this is it");HTML:
<form> <input name="email" disabled="disabled" /> <input name="id" /> </form> -
each( Function callback ) returns jQuery
Execute a function within the context of every matched element.
This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element.
Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index).
Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).Example:
Iterates over three divs and sets their color property.
$(document.body).click(function () { $("div").each(function (i) { if (this.style.color != "blue") { this.style.color = "blue"; } else { this.style.color = ""; } }); });HTML:
<div>Click here</div> <div>to iterate through</div> <div>these divs.</div>Example:
If you want to have the jQuery object instead of the regular DOM element, use the $(this) function, for example:
$("span").click(function () { $("li").each(function(){ $(this).toggleClass("example"); }); });HTML:
To do list: <span>(click here to change)</span> <ul> <li>Eat</li> <li>Sleep</li> <li>Be merry</li> </ul>Example:
You can use 'return' to break out of each() loops early.
$("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); });HTML:
<button>Change colors</button> <span></span> <div></div> <div></div> <div></div> <div></div> <div id="stop">Stop here</div> <div></div> <div></div> <div></div> -
element( String element ) returns Array<Element>
Matches all elements with the given name.
Example:
Finds every DIV element.
$("div").css("border","3px solid red");HTML:
<div>DIV1</div> <div>DIV2</div> <span>SPAN</span> -
empty( ) returns Array<Element>
Matches all elements that have no children (including text nodes).
Example:
Finds all elements that are empty - they don't have child elements or text.
$("td:empty").text("Was empty!").css('background', 'rgb(255,220,200)');HTML:
<table border="1"> <tr><td>TD #0</td><td></td></tr> <tr><td>TD #2</td><td></td></tr> <tr><td></td><td>TD#5</td></tr> </table> -
empty( ) returns jQuery
Remove all child nodes from the set of matched elements.
Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data.Example:
Removes all child nodes (including text nodes) from all paragraphs
$("button").click(function () { $("p").empty(); });HTML:
<p> Hello, <span>Person</span> <a href="javascript:;">and person</a> </p> <button>Call empty() on above paragraph</button> -
enabled( ) returns Array<Element>
Matches all elements that are enabled.
Example:
Finds all input elements that are enabled.
$("input:enabled").val("this is it");HTML:
<form> <input name="email" disabled="disabled" /> <input name="id" /> </form> - end( ) re