22 March 2015

JQuery Quick Reference

JQuery Quick Reference

1.    JQuery
            jQuery is a JavaScript Library.
jQuery greatly simplifies JavaScript programming.
jQuery uses CSS syntax to select elements.

  2. Uses
The jQuery library contains the following features:

            HTML/DOM manipulation
            CSS manipulation
            HTML event methods
            Effects and animations
If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).
   // jQuery methods go here...
   // jQuery methods go here...
This is to prevent any jQuery code from running before the document is finished loading (is ready).

3. JQuery Selector
$("*")     Selects all elements    
$(this)     Selects the current HTML element    
$("p.intro")     Selects all elements with class="intro"    
$("p:first")     Selects the first element     Try it
$("ul li:first")     Selects the first
  • element of the first
  • $("ul li:first-child")     Selects the first
  • element of every
  • $("[href]")     Selects all elements with an href attribute     Try it
    $("a[target='_blank']")     Selects all elements with a target attribute value equal to "_blank"    
    $(":button")     Selects all
    $("tr:even")     Selects all even elements    
    $("tr:odd")     Selects all odd elements


    $("p").click(function(){  // When a click event fires on a element
                $(this).hide();   //   hide the current element
                alert("You entered p1!");

                alert("You entered p1!");

                $(this).css("background-color", "#cccccc");
                $(this).css("background-color", "#ffffff");
    jQuery toggle()
    Shown elements are hidden and hidden elements are shown:

    5. jQuery Callback Functions         
    JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors.
    A callback function is executed after the current effect is finished.

                $("p").hide("slow", function(){
                alert("The paragraph is now hidden");
    function do_a( callback ){
      setTimeout( function(){
                // simulate a time consuming function
                console.log( '`do_a`: this takes longer than `do_b`' );

                // if callback exist execute it
                callback && callback();
      }, 3000 );

    function do_b(){
      console.log( '`do_b`: now we can make sure `do_b` comes out after `do_a`' );

    do_a( function(){

    Example :
    `do_a`: this takes longer than `do_b`
    `do_b`: now we can make sure `do_b` comes out after `do_a`

    6. jQuery - Chaining

    Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.
    $("#p1").css("color", "red")

    7. Get Attributes - attr()
    8.jQuery - Set Content and Attributes
                text() - Sets or returns the text content of selected elements
                html() - Sets or returns the content of selected elements (including HTML markup)
                val() - Sets or returns the value of form fields

                $("#test1").text("Hello world!");
                $("#test2").html("Hello world!");
                $("#test3").val("Dolly Duck");

    9.Set Attributes - attr()
    The jQuery attr() method is also used to set/change attribute values.

                $("#w3s").attr("href", "http://www.w3schools.com/jquery");

    10.jQuery - Add Elements
        With jQuery, it is easy to add new elements/content.
                append() - Inserts content at the end of the selected elements
                prepend() - Inserts content at the beginning of the selected elements
                after() - Inserts content after the selected elements
                before() - Inserts content before the selected elements

    11.jQuery - Remove Elements

        With jQuery, it is easy to remove existing HTML elements.

                remove() - Removes the selected element (and its child elements)
                empty() - Removes the child elements from the selected element

    jQuery remove() Method
    The jQuery remove() method removes the selected element(s) and its child elements.

    jQuery empty() Method
    The jQuery empty() method removes the child elements of the selected element(s).

    Filter the Elements to be Removed

    jQuery - Get and Set CSS Classes

    With jQuery, it is easy to manipulate the CSS of elements.

                addClass() - Adds one or more classes to the selected elements
                removeClass() - Removes one or more classes from the selected elements
                toggleClass() - Toggles between adding/removing classes from the selected elements
                css() - Sets or returns the style attribute

    jQuery addClass() Method
                $("h1, h2, p").addClass("blue");

    jQuery removeClass() Method

                $("h1, h2, p").removeClass("blue");

    jQuery toggleClass() Method

                $("h1, h2, p").toggleClass("blue");

    jQuery css() Method

    Return a CSS Property
    The following example will return the background-color value of the FIRST matched element:

    Set a CSS Property
    $("p").css("background-color", "yellow");

    Set Multiple CSS Properties
    The following example will set a background-color and a font-size for ALL matched elements:
    $("p").css({"background-color": "yellow", "font-size": "200%"});

    jQuery - Dimensions

    jQuery has several important methods for working with dimensions:


    jQuery Traversing - Ancestors
    An ancestor is a parent, grandparent, great-grandparent, and so on.
    With jQuery you can traverse up the DOM tree to find ancestors of an element.

    jQuery parent() Method
    The parent() method returns the direct parent element of the selected element.
    This method only traverse a single level up the DOM tree.

    jQuery parents() Method
    The parents() method returns all ancestor elements of the selected element, all the way up to the document's root element ().
    The following example returns all ancestors of all elements that are

    jQuery parentsUntil() Method
    The parentsUntil() method returns all ancestor elements between two given arguments.

    jQuery Traversing - Descendants

    A descendant is a child, grandchild, great-grandchild, and so on.
    With jQuery you can traverse down the DOM tree to find descendants of an element.

    jQuery children() Method

    The children() method returns all direct children of the selected element.

    jQuery find() Method
    The find() method returns descendant elements of the selected element, all the way down to the last descendant.

    The following example returns all descendants of

    jQuery siblings() Method
    The following example returns all sibling elements of


    jQuery next() Method

    The next() method returns the next sibling element of the selected element

    jQuery nextAll() Method
    The nextAll() method returns all next sibling elements of the selected element.
    jQuery nextUntil() Method
    The nextUntil() method returns all next sibling elements between two given arguments.

    jQuery first() Method
    The first() method returns the first element of the selected elements.
                $("div p").first();

    jQuery last() Method
    The last() method returns the last element of the selected elements.
                $("div p").last();
    jQuery eq() method
    The eq() method returns an element with a specific index number of the selected elements.

    jQuery filter() Method
    The following example returns all elements with class name "intro":

    jQuery not() Method
    The following example returns all elements that do not have class name "intro":

    jQuery - The noConflict() Method
    If two different frameworks are using the same shortcut, one of them might stop working.
    The jQuery team have already thought about this, and implemented the noConflict() method.
    The noConflict() method releases the hold on the $ shortcut identifier, so that other scripts can use it.

                jQuery("p").text("jQuery is still working!");

    //MVC JQuery Ajax
      data : {
                data1: data1,
                data2: data2,

      success : function(result){
      error : function(){

    Posting Javascript Object

    type     This is type of HTTP Request and accepts a valid HTTP verb. POST is the option illustrated in this article.
    url     This is the location of the resource that the request will be made to.
    data     This is the actual data to be sent as part of the request.
    contentType     This is the content type of the request you are making. The default is 'application/x-www-form-urlencoded'.
    dataType     This is the type of data you expect to receive back. Accepted values are text, xml, json, script, html jsonp. If you do not provide a value, jQuery will examine the MIME type of the response and base its decision on that.

    No comments:

    Post a Comment

    Comments Welcome