Element of Commercial Portal Unit III
Element of Commercial Portal Unit III
The purpose of jQuery is to make it much easier to use JavaScript on your website.
Before you start studying jQuery, you should have a basic knowledge of:
HTML
CSS
JavaScript
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish,
and wraps them into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM
manipulation.
HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
There are lots of other JavaScript libraries out there, but jQuery is probably the most popular,
and also the most extendable.
Many of the biggest companies on the Web use jQuery, such as:
Google
Microsoft
IBM
Netflix
Examples:
$(document).ready(function(){
});
This is to prevent any jQuery code from running before the document is finished loading (is
ready).
It is good practice to 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.
Here are some examples of actions that can fail if methods are run before the document is fully
loaded:
$(function(){
});
Use the syntax you prefer. We think that the document ready event is easier to understand when
reading the code.
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s).
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.
All selectors in jQuery start with the dollar sign and parentheses: $().
$("p")
Example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
An id should be unique within a page, so you should use the #id selector when you want to find a
single, unique element.
To find an element with a specific id, write a hash character, followed by the id of the HTML
element:
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<button>Click me</button>
</body>
</html>
Output:
This is a heading
This is a paragraph.
Click me
Example - 2
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
Output:
To find elements with a specific class, write a period character, followed by the name of the
class:
$(".test")
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.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
</script>
</head>
<body>
<button>Click me</button>
</body>
Output:
This is a heading
This is a paragraph.
Click me
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<body>
</body>
</html>
Output:
Syntax Description
$("ul li:first") Selects the first <li> element of the first <ul>
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to
"_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal
to "_blank"
jQuery Traversing
jQuery traversing, which means "move through", are used to "find" (or select) HTML elements
based on their relation to other elements. Start with one selection and move through that
selection until you reach the elements you desire.
The image below illustrates an HTML page as a tree (DOM tree). With jQuery traversing, you
can easily move up (ancestors), down (descendants) and sideways (siblings) in the tree, starting
from the selected (current) element. This movement is called traversing - or moving through - the
DOM tree.
Illustration explained:
The <div> element is the parent of <ul>, and an ancestor of everything inside of it
The <ul> element is the parent of both <li> elements, and a child of <div>
The left <li> element is the parent of <span>, child of <ul> and a descendant of <div>
The <span> element is a child of the left <li> and a descendant of <ul> and <div>
The two <li> elements are siblings (they share the same parent)
The right <li> element is the parent of <b>, child of <ul> and a descendant of <div>
The <b> element is a child of the right <li> and a descendant of <ul> and <div>
The next chapters will show us how to travel up, down and sideways in the DOM tree.
parent()
parents()
parentsUntil()
The following example returns the direct parent element of each <span> elements:
<html>
<head>
<style>
.ancestors * {
display: block;
color: lightgrey;
padding: 5px;
margin: 15px;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
11
$(document).ready(function(){
});
</script>
</head>
<body>
<div class="ancestors">
<ul>ul (grandparent)
<span>span</span>
</li>
</ul>
</div>
<span>span</span>
</p>
</div>
</div>
</body>
</html>
<html>
<head>
<style>
.ancestors * {
display: block;
color: lightgrey;
padding: 5px;
margin: 15px;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
});
</script>
</head>
<ul>ul (grandparent)
<span>span</span>
</li>
</ul>
</div>
</body>
<!-- The outer red border, before the body element, is the html element (also an ancestor) -->
</html>
children()
find()
This method only traverses a single level down the DOM tree.
The following example returns all elements that are direct children of each <div> elements:
<html>
<head>
<style>
.descendants * {
color: lightgrey;
padding: 5px;
margin: 15px;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
});
</script>
</head>
<body>
<p>p (child)
<span>span (grandchild)</span>
</p>
<p>p (child)
<span>span (grandchild)</span>
</p>
</div>
</html>
siblings()
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()
<html>
<head>
<style>
.siblings * {
display: block;
color: lightgrey;
padding: 5px;
margin: 15px;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
});
</script>
</head>
<body class="siblings">
<div>div (parent)
<p>p</p>
<span>span</span>
<h3>h3</h3>
<p>p</p>
</div>
</body>
</html>
Other filtering methods, like filter() and not() allow you to select elements that match, or do not
match, a certain criteria.
Example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").first().css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p>This is a paragraph.</p>
</div>
<br>
</div>
<br>
</div>
</body>
</html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color", "yellow");
});
</script>
<body>
<h1>Welcome to My Homepage</h1>
</body>
</html>
This method lets you specify a criteria. Elements that do not match the criteria are removed from
the selection, and those that match will be returned.
Syntax
$(selector).filter(criteria,function(index))
Parameter Description
function(index) Optional. Specifies a function to run for each element in the set. If it returns true,
the element is kept. Otherwise, the element is removed.
index - The index position of the element in the set
Note: this is the current DOM element.