Getting Started with jQuery


Background:

With the popularity of World Wide Web, scripting languages likeJavaScript became very popular. Mostly because it allows to read and write values in different DOM (Document Object Model) elements, handle different events without even visiting the server. In short without JavaScript web-based programming is incomplete.

Along with all these advantages, there are a few disadvantages of JavaScript as well, cross browser behavioural difference, complex syntax are a few disadvantages to name.


Introduction:

In 2006 jQuery was introduced, which not only addressed the disadvantages of JavaScript but also added many more features.To start with the introduction of jQuery, first we have to know what jQuery is. Actually it is not a language but a library of well written JavaScript code.

Let’s have a look at a few advantages we get by using jQuery:

  • Cross-browser behaviour of the client side code remains same
  • Amount of code we need to write to achieve a specific functionality in less compared to JavaScript
  • Syntax are a lot simpler
  • JSON parsing
  • We can extend jQuery to create our own UI Component

Apart from the above, there are many more advantages some are:

  • Interacting with the server through asynchronous calls
  • Basic animation in the browser
  • Faster execution

How to enable jQuery?

jQuery is free, open source software Dual-licensed under the MIT License and the GNU General Public License. We can simply download from the internet. Microsoft has integrated jQuery officially into its IDE Visual Studio from the 2010 version onwards and jQuery intellisense and debugging is available in Visual Studio now.

It comes with a single file that includes all its interfaces, including DOM, events and Ajax Function. To use jQuery in our code, all we have to do is adding the jQuery file as script source to the HTML page. We can either download the file and add that as script source in our HTML file, or use CDNas the source in the HTML.


Download jQuery:

We can download the jQuery file from jQuery.com. There are development version and minified versions (compressed), any one of that can be downloaded and referred like below:
<head>
<scriptsrc="jquery-3.3.1.min.js"></script>
</head>

Minified version is smaller in size, so loading time is less, hencecan be used for better performance in production environment. But for development and testing purpose generally normal uncompressed and readable version is used to get more precise error and better understanding of the library.

The CDN:

Both Google and Microsoft host jQuery which we can directly refer in our page like:

The Google CDN:

<head>
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

The Microsoft CDN:
<head>
<scriptsrc="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>


The Syntax:

While programming with jQuery the basic goal is to select one/multiple DOM element and the do some action on that. This action can be:
  • Reading value of that element
  • Writing value to that element
  • Change the CSS of that element
  • Manipulate different HTML attributes of that element
  • Assign different actions for that element

The sign $ denotes a jQuery constructor. It can be written either as $() or jQuery(). All jQuery constructors must start with the $() or jQuery().

So the basic jQuery syntax is $(selector).action().

In the above syntax,

  • $ Shortcut is basically used to define/access jQuery.
  • Selector is basically a query to find one/many HTML element.
Here are a few examples:

•   varmyValue = $("#test").val();//get the value in the HTML element with id “test”
•   $(“.test”).hide();//hide all HTML elements that have a css class “test”
•   $("#test").click(function(){
//code here
    })// do certain things on the click event of HTML element with id “test”



The Ready Event

As discussed already, jQuery is used to access the DOM elements. So what will happen if we try to use jQuery code to access a DOM element even before the DOM is loaded? The answer is simple this will simply not work.

In order to ensure that our jQuery code executes after all the DOM element is loaded we have to write all the jQuery code within the $(document).ready function.

Let’s have a look at the example below,

<html>
<head>
<scriptsrc="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>
<body>
<divid="TESTdIV"></div>
</body>
</html>
<scripttype="text/javascript">
$(document).ready(function()
    {
$("#TESTdIV").text("Hello, world!");    
    });
</script>

The above code will wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.

In real world situation where we may have to work with a long list of things to be done once the DOM is loaded we may opt to make that as a separate function and call from the ready event. The example below demonstrates that,

<scripttype="text/javascript">
functioninit(){
$("#TESTdIV").text("Hello, world!");
    }

$(document).ready(init);
</script>


The $(document).ready have even a shorter syntax like :

$(function(){
// jQuery methods go here...
});

Next

I hope by now we have got the basic understanding about jQuery, what it is, what advantages we can get out of it, and how to start using jQuery and what are the things we can achieve by using jQuery.

Next we will see different ways to select DOM elements so that we can playing around with DOM elements

Comments

Popular posts from this blog

Handling Events in jQuery – Few Advanced Notes and Faqs

Ways to select DOM element(s) - a guide to jQuery Selectors

Let's traverse through DOM Tree - Few advanced notes on jQuery Selectors