jquery
jquery
JQuery
jQuery is a lightweight, "write less, do more", JavaScript library.
• jQuery is free, open-source software using the permissive MIT License. The MIT
License is a permissive free software license originating at the Massachusetts Institute
of Technology (MIT) in the late 1980s.
• jQuery's syntax is designed to make it easier to navigate a document, select DOM
elements, create animations, handle events, and develop Ajax applications.
• This enables developers to create abstractions for low-level interaction and animation,
advanced effects and high-level, theme-able widgets.
• jQuery is a fast, small, cross-platform and feature-rich JavaScript library. It is designed to
simplify the client-side scripting of HTML.
The main purpose of jQuery is to provide an easy way to use JavaScript on your
website to make it more interactive and attractive. It is also used to add animation.
• JQuery is also very useful to simplify a lot of the complicated things from JavaScript,
like AJAX calls and DOM manipulation.
Features supported by jQuery:
1. Simple and Easy: JavaScript have predefined methods using we can perform any
4. CSS Manipulation: jQuery have predefined css() method for manipulate style for
5. HTML Manipulation: The jQuery made it easy to select DOM elements, traverse
6. Extendibility: We start out with a core library and extend its functionality per our
2. For larger and more complicated applications we will need to extend way beyond
3. jQuery is easy to install and learn, initially. But it’s not that easy if we compare it
with CSS.
JQUERY LIBRARIES
2. Brevity and Clarity: jQuery promotes brevity and clarity with features like
"chainable" functions and shorthand function names.
4. Extensibility: New events, elements, and methods can be easily added and then
reused as a plug-in.
The jQuery uses the Document Object Model (DOM) to manipulate, traverse, and select
elements.
• The HTML document is loaded onto the DOM, where the browser creates a node tree
when the page loads.
• A familial, hierarchical relationship is formed with the elements on the tree where
elements are parents, children and siblings of each other.
INCLUDING JQUERY LIBRARY IN PAGE
<html>
<head>
<title>First jQuery Example</title>
<script type = "text/javascript"
src = "/jquery/jquery-2.1.3.min.js">
</script>
<script type = "text/javascript">
$(document).ready(function()
{
document.write("Hello, jQuery World!");
});
</script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
CDN Based Version of JQuery:
• We can include jQuery library into the HTML code directly from Content Delivery
Network (CDN).
• Google and Microsoft provides content deliver for the latest version. Now let us
rewrite above example using jQuery library from Google CDN.
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript">
$(document).ready(function()
{
document.write("Hello, jQuery World!");
});
</script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
We might have noticed that all jQuery methods in our
examples, are inside a
document ready event:
$(document).ready(function()
{
// jQuery methods go here...
});
• This is to prevent any jQuery code from running before the
document is finished
loading (is ready).
JQUERY SELECTORS
• jQuery selectors are one of the most important parts of the jQuery library. jQuery
Selectors are used to select and manipulate HTML elements.
• jQuery selectors are used to "find" (or select) HTML elements based on their name, id,
classes, types, attributes, values of attributes and much more.
• It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
• A jQuery Selector is a function which makes use of expressions to find out matching
elements from a DOM based on the given criteria.
• Simply we can say, selectors are used to select one or more HTML elements using
jQuery. Once, an element is selected then we can perform various operations on that
selected element.
• All jQuery selectors start with a dollor sign and parenthesis e.g. $(). It is known as the
factory function.
• The factory function $() makes use of following three building blocks while selecting
elements in a given document:
1. Tag Name represents a tag name available in the DOM. For example $('p') selects
all paragraphs <p> in the document.
2. Tag ID represents a tag available with the given ID in the DOM. For example
$('#some-id') selects the single element in the document that has an ID of some-id.
3. Tag Class represents a tag available with the given class in the DOM. For example
$('.some-class') selects all elements in the document that have a class of some-class.
Following is an example which makes use of Tag Selector. This would select all the
elements with a tag name p.
<html><head>
<title>jQuery Example</title>
<script type = "text/javascript"
src ="https://ajax.googleapis.com/ajax/libs/
jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript"
language = "javascript">
$(document).ready(function()
{
$("p").css("background-color", "red");
});
</script>
</head>
<body>
<div>
<p class = "myclass">This is a 1st paragraph...</p>
<p id = "myid">This is 2nd paragraph...</p>
<p>This is 3rd paragraph...</p>
</div>
</body></html>
How to Use Selectors?
• The selectors in JQuery are very useful and would be required
at every step while
using jQuery. They get the exact element that we want from
the HTML document.
• Few basic selectors in JQuery are explained below:
o Name selector selects all elements which match with the
given element Name.
o #ID selector selects a single element which matches with the
given ID.
o .Class selector selects all elements which match with the
given Class.
o Universal (*) selector selects all elements available in a DOM.
1. The element Selector in jQuery:
• The jQuery element selector selects elements based on the element name.
• The syntax is, $('tagname')
• Example, $("p")
In following example, when a user clicks on a button, all <p> elements will be hidden:
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/
jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This program shows Hiding of paragraph</h2>
<p>This is a first paragraph.</p>
<p>This is another paragraph.</p>
<button>Click this button to hide above paragraphs</button>
</body></html>
2. The #id Selector in jQuery:
• The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.
• An id should be unique within a page, so we should use the #id selector when we want
to find a single, unique element.
• The syntax is, $('elementid').
• Example, to find an element with a specific id, write a hash character, followed by the
id of the HTML element such as, $("#test").
• In following example, when a user clicks on a button, the element with id="test" will
be hidden:
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
}); });
</script></head>
<body>
<h2>Example of Jquery using id</h2>
<p>This is a first paragraph code.</p>
<p id="test">This is another paragraph code.</p>
<p>This is a Third paragraph code.</p>
<button>Click this Button</button>
</body></html>
3. The .class Selector in jQuery:
• The jQuery .class selector finds elements with a specific class.
• The syntax is, $('.classid')
• Example, to find an element with a specific class, write a period character, followed by
the name of the class such as, $(".test")
• In following example, when a user clicks on a button, the elements with class="test"
will be hidden:
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/
jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".tmp").hide();
}); });
</script>
</head>
<body>
<h2 class="tmp">This is a JQuery heading</h2>
<p class="tmp">This is a First Line.</p>
<p>This is Last Line.</p>
<button>Click this Button</button>
</body></html>
DOCUMENT OBJECT MODEL (DOM) MANIPULATION
• JQuery provides methods like such as .attr(), .html() etc., to manipulate DOM in efficient way.
Definition of DOM:
“The Document Object Model (DOM) is an application programming interface (API) for HTML and XML documents.
It defines the logical structure of documents and the way a document is accessed and manipulated.”
• DOM defines the objects and properties and methods (interface) to access all XML elements.
It is separated into following three different parts / levels:
Output:
This is Line one .
This is Line two.
This is Line three.
• jQuery provides various methods to add, edit or delete DOM element(s) in the HTML page.
• jQuery provides methods to manipulate DOM in efficient way. We do not need to write
big code to modify the value of any element's attribute or to extract HTML code from a
paragraph or division.
• jQuery provides methods such as .attr(), .html() and .val() which act as getters, retrieving
information from DOM elements for later use.
• jQuery attr() method is used to get or set the value of specified attribute of DOM
element. jQuery html() method sets or returns the content of selected elements
(including HTML markup).
• jQuery val() method sets or returns the value of form fields. jQuery text() method sets
or returns the text content of selected elements.
Adding New HTML Content:
1. append() method inserts content at the end of the selected elements.
2. prepend() method inserts content at the beginning of the selected elements.
3. after() method inserts content after the selected elements.
4. before() method inserts content before the selected elements.
Removing Elements/Content:
• With jQuery, it is easy to remove existing HTML elements. To remove elements and
content, there are following two jQuery methods:
1. remove() method removes the selected element (and its child elements).
2. empty() method removes the child elements from the selected element.
DOM Element Replacement:
• We can replace a complete DOM element with the specified HTML or DOM elements.
The replaceWith(content) method serves this purpose very well.
Syntax:
selector.replaceWith(content)
<html>
<head>
<title>The jQuery Example for replace division element </title>
<script src ="jquery.js"></script>
<script>
$(document).ready(function()
{
$("div").click(function ()
{
$(this).replaceWith("<h1>PHP using JQuery is Easy</h1>");
});
});
</script>
<style>
#division
{
margin:20px;
padding:22px;
border:12px solid #666;
width:70px;
}
</style>
</head>
<body>
<p>Click on the square below:</p>
<span id = "result"> </span>
<div id = "division" style = "background-color:pink;">
This is Pink Square!!
</div>
</body>
</html>
Removing DOM Elements:
• jQuery provides various methods to add, edit or delete DOM element(s) in the HTML page.
1. The width() method sets or returns the width of an element (excludes padding, border and margin).
2. The height() method sets or returns the height of an element (excludes padding,
border and margin).
3. The innerWidth() method returns the width of an element (includes padding).
4. The innerHeight() method returns the height of an element (includes padding).
5. The outerWidth() method returns the width of an element (includes padding and border).
6. The outerHeight() method returns the height of an element (includes padding
and border).
7. The offset() method gets or sets left and top coordinates of the specified
element(s).
8. The position() method gets the current coordinates of the specified element(s).
<html><head>
<script src=“jquery.js"></script> <body>
<script> <div id="div1"></div>
$(document).ready(function(){ <br>
$("button").click(function(){ <button>Display Dimensions of div tag</button>
var tmp = ""; <p>width() - This method gives the width of an
tmp += "Width of div tag: " + $("#div1").width() + "</br>"; element.</p>
tmp += "Height of div tag: " + $("#div1").height();
$("#div1").html(tmp); <p>height() - This method gives the height of an
}); element.</p>
});
</script> </body>
<style> </html>
#div1 {
height: 120px;
width: 350px;
padding: 20px;
margin: 5px;
background-color: orange;
border: 3px solid green;
}
</style></head>