Web Designing Tutorials

How to Use jQuery With HTML?

Google+ Pinterest LinkedIn Tumblr
This single global function with two names is the central query function for jQuery. For example, if you need to ask for the set of all <div> elements in a document, write the following code.

var divs=$("div");

The value returned by the jQuery() function represents a set of zero or more DOM elements is known as a jQuery object. It defines many methods for operating on the sets of elements they represent.

How to Obtain jQuery

jQuery library is free software. You can download it from its official website. Once you have download the code, you can include it in your web pages with a <script> element as given below.

<script src="jquery-1.11.2.min.js"></script>

And another way to use jQuery in your web application is to allow a Content Distribution Network (CDN) to serve it using a URL like one of these.

Read Also: How to Alter HTML Document Structure using jQuery?

Google CDN

https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js

Microsoft CDN

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.3.min.js
http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js

jQuery CDN

http://code.jquery.com/jquery-2.1.3.min.js
http://code.jquery.com/jquery-1.11.2.min.js

If you use Google CDN, you can use “1.11” to get the latest release in the 1.11.x series, or just “1” to get the most current release less than 2.0.

The major advantage of loading jQuery from well-known URLs like these is that, because of jQuery’s popularity, visitors to your website will likely already have a copy of the library in the browser’s cache and no download will be necessary.

Read Also: How to Create Animated Visual Effects Using jQuery

How to Use jQuery() Function

The jQuery() function is the most important one in its library. It is heavily overloaded, however, and there are four different ways you can invoke it.

The first and the most common way to invoke $() is to pass a CSS selector to it. Which returns the set of elements from the current document that match the selector as given in the example below.

var id=$("#id");
var class=$(".class");

And another way to invoke $() is to pass it an Element or Document or Window object. It simply wraps the element, document or window in a jQuery object and returns that object. Doing this allows you to use its methods to manipulate the element rather than using raw DOM methods. For example you can call $(document) or $(this) to pass document or current element.

The third way to invoke $() is to pass it a string of HTML text. When you do this, jQuery creates the HTML element or elements described by that text and then returns a jQuery object representing those elements.

Read Also: How to Scroll Top or Bottom of Document Using JavaScript

When invoked in this way, $() accepts an optional second argument. You can pass a document object to specify the document with which the elements are to be associated. If you do this, the object properties are assumed to specify the names and values of HTML attributes to be set on the object.

Here is an example, where we will use three arguments to set the URL, CSS of the image and the click event on the image.

var img=$("<img />"),
    {src:url,
    css:{borderWidth:5},
    click:handleClick
    });

And the fourth way to invoke $() is to pass a function to it. If you do this, the function you pass will be invoked when the document has been loaded and the DOM is ready to be manipulated. For example here is a jQuery version of the onLoad() function which will invoke when the document has loaded.

Read Next: How to Get and Set Element Attributes using jQuery

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 7 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment