Jquery - Quick Guide, Reference Manual
Jquery - Quick Guide, Reference Manual
com
Advertisements
This page lists down all the jQuery APIs at one place for your easy access. So my recommendation is to bookmark for future use.
jQuery - Selectors
Following table lists down few basic selectors and explains them with examples. Selector Name #ID .Class Universal (*) Multiple Elements E, F, G Description Selects all elements which match with the given element Name. Selects a single element which matches with the given ID Selects all elements which match with the given Class. Selects all elements available in a DOM. Selects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors: $('*'): This selector selects all elements in the document. $("p > *"): This selector selects all elements that are children of a paragraph element. $("#specialID"): This selector function gets the element with id="specialID". $(".specialClass"): This selector gets all the elements that have the class of specialClass. $("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass". $("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass. $("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements. $("ul li:first"): This selector gets only the first <li> element of the <ul>. $("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container . $("li > ul"): Selects all elements matched by <ul> that are children of an element matched
by <li> $("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>. $("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>. $("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>. $("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that have a class of myclass. $(":empty"): Selects all elements that have no children. $("p:empty"): Selects all elements matched by <p> that have no children. $("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>. $("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass. $("a[@rel]"): Selects all elements matched by <a> that have a rel attribute. $("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname. $("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname. $("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar $("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com. $("li:even"): Selects all elements matched by <li> that have an even index value. $("tr:odd"): Selects all elements matched by <tr> that have an odd index value. $("li:first"): Selects the first <li> element. $("li:last"): Selects the last <li> element. $("li:visible"): Selects all elements matched by <li> that are visible. $("li:hidden"): Selects all elements matched by <li> that are hidden. $(":radio"): Selects all radio buttons in the form. $(":checked"): Selects all checked boxex in the form. $(":input"): Selects only form elements (input, select, textarea, button). $(":text"): Selects only text elements (input[type=text]). $("li:eq(2)"): Selects the third <li> element $("li:eq(4)"): Selects the fifth <li> element $("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other
words, the first two <li> elements. $("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements. $("li:gt(1)"): Selects all elements matched by <li> after the second one. $("p:gt(2)"): Selects all elements matched by <p> after the third one. $("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>. $("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>. $("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p> $("li:first-child"): Selects all elements matched by <li> that are the first child of their parent. $("li:last-child"): Selects all elements matched by <li> that are the last child of their parent. $(":parent"): Selects all elements that are the parent of another element, including text. $("li:contains(second)"): Selects all elements matched by <li> that contain the text second.
val( val )
Set the value attribute of every matched element if it is called on <input> but if it is called on <select> with the passed <option> value then passed option would be selected, if it is called on check box or radio box then all the matching check box and radiobox would be checked.
Similar to above syntax and examples, following examples would give you understanding on using various attribute methods in different situation: $("#myID").attr("custom") : This would return value of attribute custom for the first element matching with ID myID. $("img").attr("alt", "Sample Image"): This sets the alt attribute of all the images to a new value "Sample Image". $("input").attr({ value: "", title: "Please enter a value" }); : Sets the value of all <input> elements to the empty string, as well as sets the title to the string Please enter a value. $("a[href^=http://]").attr("target","_blank"): Selects all links with an href attribute starting with http:// and set its target attribute to _blank $("a").removeAttr("target") : This would remove target attribute of all the links. $("form").submit(function() {$(":submit",this).attr("disabled", "disabled");}); : This would modify the disabled attribute to the value "disabled" while clicking Submit button. $("p:last").hasClass("selected"): This return true if last <p> tag has associated classselected. $("p").text(): Returns string that contains the combined text contents of all matched <p> elements. $("p").text("<i>Hello World</i>"): This would set "<I>Hello World</I>" as text content of the matching <p> elements $("p").html() : This returns the HTML content of the all matching paragraphs. $("div").html("Hello World") : This would set the HTML content of all matching <div> to Hello World. $("input:checkbox:checked").val() : Get the first value from a checked checkbox $("input:radio[name=bar]:checked").val(): Get the first value from a set of radio buttons $("button").val("Hello") : Sets the value attribute of every matched element <button>. $("input").val("on") : This would check all the radio or check box button whose value is "on". $("select").val("Orange") : This would select Orange option in a dropdown box with options Orange, Mango and Banana. $("select").val("Orange", "Mango") : This would select Orange and Mango options in a dropdown box with options Orange, Mango and Banana.
list of DOM elements: Selector eq( index ) filter( selector ) filter( fn ) Description Reduce the set of matched elements to a single element. Removes all elements from the set of matched elements that do not match the specified selector(s). Removes all elements from the set of matched elements that do not match the specified function. Checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector. Translate a set of elements in the jQuery object into another set of values in a jQuery array (which may, or may not contain elements). Removes elements matching the specified selector from the set of matched elements. Selects a subset of the matched elements.
is( selector )
map( callback )
Following table lists down other useful methods which you can use to locate various elements in a DOM: Selector add( selector ) andSelf( ) children( [selector]) closest( selector ) contents( ) end( ) find( selector ) next( [selector] ) nextAll( [selector] ) offsetParent( ) parent( [selector] ) Description Adds more elements, matched by the given selector, to the set of matched elements. Add the previous selection to the current selection. Get a set of elements containing all of the unique immediate children of each of the matched set of elements. Get a set of elements containing the closest parent element that matches the specified selector, the starting element included. Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe. Revert the most recent 'destructive' operation, changing the set of matched elements to its previous state . Searches for descendent elements that match the specified selectors. Get a set of elements containing the unique next siblings of each of the given set of elements. Find all sibling elements after the current element. Returns a jQuery collection with the positioned parent of the first matched element. Get the direct parent of an element. If called on a set of elements, parent returns a set of their unique direct parent elements. Get a set of elements containing the unique ancestors of the
matched set of elements (except for the root element). Get a set of elements containing the unique previous siblings of each of the matched set of elements. Find all sibling elements in front of the current element. Get a set of elements containing all of the unique siblings of each of the matched set of elements.
width( )
Get the current computed, pixel, width of the first matched element.
Wrap the inner child contents of each matched element (including text nodes) with a DOM element. Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
jQuery - AJAX
Following is the list of useful AJAX Methods: Methods and Description
jQuery.ajax( options ) Load a remote page using an HTTP request. jQuery.ajaxSetup( options ) Setup global settings for AJAX requests. jQuery.get( url, [data], [callback], [type] ) Load a remote page using an HTTP GET request. jQuery.getJSON( url, [data], [callback] ) Load JSON data using an HTTP GET request. jQuery.getScript( url, [callback] ) Loads and executes a JavaScript file using an HTTP GET request. jQuery.post( url, [data], [callback], [type] ) Load a remote page using an HTTP POST request. load( url, [data], [callback] ) Load HTML from a remote file and inject it into the DOM. serialize( ) Serializes a set of input elements into a string of data. serializeArray( ) Serializes all forms and form elements like the .serialize() method but returns a JSON data structure for you to work with. Based on different events/stages following methods are available: Methods and Description ajaxComplete( callback ) Attach a function to be executed whenever an AJAX request completes. ajaxStart( callback ) Attach a function to be executed whenever an AJAX request begins and there is none already active. ajaxError( callback ) Attach a function to be executed whenever an AJAX request fails. ajaxSend( callback ) Attach a function to be executed before an AJAX request is sent. ajaxStop( callback ) Attach a function to be executed whenever all AJAX requests have ended. ajaxSuccess( callback ) Attach a function to be executed whenever an AJAX request completes successfully.
jQuery - Effects
Following table lists down all the important methods to create different kind of effects: Methods and Description animate( params, [duration, easing, callback] ) A function for making custom animations. animate( params, options )
A function for making custom animations. fadeIn( speed, [callback] ) Fade in all matched elements by adjusting their opacity and firing an optional callback after completion. fadeOut( speed, [callback] ) Fade out all matched elements by adjusting their opacity to 0, then setting display to "none" and firing an optional callback after completion. fadeTo( speed, opacity, callback ) Fade the opacity of all matched elements to a specified opacity and firing an optional callback after completion. hide( ) Hides each of the set of matched elements if they are shown. hide( speed, [callback] ) Hide all matched elements using a graceful animation and firing an optional callback after completion. show( ) Displays each of the set of matched elements if they are hidden. show( speed, [callback] ) Show all matched elements using a graceful animation and firing an optional callback after completion. slideDown( speed, [callback] ) Reveal all matched elements by adjusting their height and firing an optional callback after completion. slideToggle( speed, [callback] ) Toggle the visibility of all matched elements by adjusting their height and firing an optional callback after completion. slideUp( speed, [callback] ) Hide all matched elements by adjusting their height and firing an optional callback after completion. stop( [clearQueue, gotoEnd ]) Stops all the currently running animations on all the specified elements. toggle( ) Toggle displaying each of the set of matched elements. toggle( speed, [callback] ) Toggle displaying each of the set of matched elements using a graceful animation and firing an optional callback after completion. toggle( switch ) Toggle displaying each of the set of matched elements based upon the switch (true shows all elements, false hides all elements). jQuery.fx.off Globally disable all animations.
After extracting jquery-ui-1.7.2.custom.min.js file from the download, you would include this file in similar way as you include core jQuery Library file. Methods and Description Blind Blinds the element away or shows it by blinding it in. Bounce Bounces the element vertically or horizontally n-times. Clip Clips the element on or off, vertically or horizontally. Drop Drops the element away or shows it by dropping it in. Explode Explodes the element into multiple pieces. Fold Folds the element like a piece of paper. Highlight Highlights the background with a defined color. Puff Scale and fade out animations create the puff effect. Pulsate Pulsates the opacity of the element multiple times. Scale Shrink or grow an element by a percentage factor. Shake Shakes the element vertically or horizontally n-times. Size Resize an element to a specified width and height. Slide Slides the element out of the viewport. Transfer Transfers the outline of an element to another.
Copyright tutorialspoint.com