jQuery - SPLessons

Chapter 6

jQuery Application

jQuery Application

jQuery Application chapter gives an idea on how to create a basic application using jQuery.

The first thing to include in jQuery Application is the jQuery library to HTML page with a <script> tag. As jQuery is nothing but a JavaScript library, jQuery code can be placed in the <script> element. However, to place in an external JavaScript file, which is preferred, remove this part.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

Now, use jQuery to manipulate this jQuery Application, and typically follow these below steps of action

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <title>jQuery First Application</title>

     <h1>Welcome to SPLessons</h1>
        <p>click on this paragraph</p>

//wait until document ready event has fired.

//select the HTML element with 'div' tag.
var theDiv = $("div");

//Traverse to 'p' element inside 'div' tag.
var theParagraph = theDiv.find("p");

//Add the click event handler on selected 'p' element.

//Modify the attributes and CSS styles of 'p' element. 
         .html("This is a paragraph")
         .css({"background-color": "#00A9EC", "font-size": "200%"});

In the above jQuery example the “p” is a jQuery selector that selects all the paragraphs. The paragraph text in the example above is given color automatically when the document is ready and performs some the action when the user clicks on the paragraph text.



jQuery Application draws out the following important points.

  • Include jQuery library in the <script> in HTML head section.
  • Add document ready function and event handling functions with required css style.