jQuery is a high-level JavaScript library which allows the developer to perform sophisticated methods with just a few lines of code. For instance, with jQuery it is possible to easily manipulate the contents of a page, perform AJAX queries or create interesting visual effects such as fading page elements in and out. The corresponding code is often significantly simpler than the raw JavaScript and DOM code that one would have to use otherwise.
Therefore, jQuery can save a lot of time and effort for certain tasks. However, it should be said that it is still worth knowing the underlying "standard" W3C JavaScript and DOM functionality, as jQuery is only one of many libraries whereas the underlying W3C libraries can be used in any client-side web application running on modern browsers.
jQuery test
]]>
Hopefully it shouldn't be too difficult to figure out what the above example is doing. We define a function called hello(), and then when the document is "ready" (i.e. it has been loaded into memory), we run the hello() function with the line:
$(document).ready(hello);
In more depth, the $ (dollar) symbol represents the jQuery object. This is an object which can contain page elements or collections of page elements, depending on the context, and allows us to perform jQuery functions on those elements. $ is actually a shorthand for jQuery, so we could equally well do:
jQuery(document).ready(hello);The ready method of a jQuery object specifies the code to run when that page element is "ready", i.e. when it has been loaded into memory. So, when the HTML document has been loaded into memory, we run the hello() function.
jQuery test
]]>
Note here how again we define a function - this time onLoad() - to run when the page is ready. This time, the function does not do anything directly (such as display "Hello!", as the first example did) but instead attaches a function to an existing page element - the button with the ID of btn1 - so that when the button is clicked, the hello() function is run.
Note the slightly different syntax to select an element with a given ID in jQuery contained to standard DOM. Whereas in the DOM we would do:
document.getElementById('btn1')
in jQuery we instead use:
$('#btn1')
Note how we use #btn1 rather than simply btn1.
This illustrates an important point about selecting page elements with
jQuery: we use CSS selector syntax, rather than DOM syntax.
Recall for instance that to style an element with a particular ID in CSS,
we use the same syntax as above:
#elem1 { background-color: black; color: yellow }
It follows that we can use other forms of CSS selector syntax in jQuery.
For instance:
$('a').click(hello);
will cause the hello() method to run when any hyperlink
('a' element) is clicked, and:
$('.important').click(hello);
will cause the hello() method to run when any page element with
a class of 'important' is clicked. Other forms of CSS selector can
be used too.
jQuery allows us to easily manipulate page elements, such as change their style or content. In addition, we can achieve some interesting effects which would require relatively complex code in raw JavaScript. In this example, the contents of a div are altered with jQuery:
jQuery test
This is the test div