Unit 4 - Full Stack Development
Unit 4 - Full Stack Development
Page
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.
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
jQuery Features
o HTML manipulation
o DOM manipulation
There are several ways to start using jQuery on your web site. You can:
Downloading jQuery
• Production version - this is for your live website because it has been minified and
compressed
• Development version - this is for testing and development (uncompressed and
readable code)
• The jQuery library is a single JavaScript file, and you reference it with the
HTML <script> tag (notice that the <script> tag should be inside
the <head> section):
• <head>
<script src="[Link]"></script>
</head>
jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a CDN
(Content Delivery Network).
Google is an example of someone who host jQuery:
<head>
<script src="[Link]
</script>
3
</head>
Page
The jQuery syntax is tailor-made for selecting HTML elements and performing
some action on the element(s).
$(document).ready(function(){
// jQuery methods go here...
});
This is to prevent any jQuery code from running before the document is finished
loading.
The jQuery team has also created an even shorter method for the document ready
event:
$(function(){
});
Page
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: $().
The jQuery element selector selects elements based on the element name.
$("p")
Example: When a user clicks on a button, all <p> elements will be hidden:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
5
<h2>This is a heading</h2>
Page
</body>
</html>
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 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:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
6
Page
});
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
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:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
7
Page
$(".test").hide();
<button>Click me</button>
</body>
</html>
8
Page
$("p").click();
The next step is to define what should happen when the event fires. You must pass a
function to the event:
$("p").click(function(){
// action goes here!!
});
1. click()
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the
current <p> element:
9
Page
</body>
</html>
2. dblclick()
The function is executed when the user double-clicks on the HTML element:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
10
$(document).ready(function(){
Page
</body>
</html>
3. mouseenter()
The function is executed when the mouse pointer enters the HTML element:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
});
</script>
11
</head>
Page
</body>
</html>
4. mouseleave()
The function is executed when the mouse pointer leaves the HTML element:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
</body>
</html>
12
Page
The function is executed, when the left, middle or right mouse button is pressed down,
while the mouse is over the HTML element:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
});
</script>
</head>
<body>
</body>
</html>
6. mouseup()
The function is executed, when the left, middle or right mouse button is released, while
the mouse is over the HTML element:
13
Page
</body>
</html>
7. hover()
The first function is executed when the mouse enters the HTML element, and the second
function is executed when the mouse leaves the HTML element:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
14
Page
</body>
</html>
15
Page
1. submit()
jQuery submit event is sent to the element when the user attempts to submit a form.
the <form> elements that is executed when the user is attempting to submit a
form. The following example will display a message depending on the value
entered when you try to submit the form.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Executing a Function on Form Submit Event in jQuery</title>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("form").submit(function(){
alert("Form is submitted successfully.");
});
});
</script>
</head>
<body>
2. Change:
The jQuery change() method attach an event handler function to
the <input>, <textarea> and <select> elements that is executed when its
value changes. The following example will display an alert message when you
16
<!DOCTYPE html>
3. focus()
The focus() method attaches an event handler function to an HTML form field.
4 . blur()
The blur() method attaches an event handler function to an HTML form field.
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
17
$(document).ready(function(){
$("input").focus(function(){
Page
</body>
</html>
18
Page
jQuery provides various methods to add, edit or delete DOM element(s) in the HTML
page. The following table lists some important methods to add/remove new DOM
elements.
19
Page
The jQuery after() method inserts content (new or existing DOM elements) after target
element(s) which is specified by a selector.
Syntax:
$('selector expression').after('content');
First of all, specify a selector to get the reference of target element(s) after which you
want to add the content and then call after() method. Pass the content string as a
parameter.
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
</script>
<script>
$(document).ready(function () {
});
Page
</script>
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
</body>
</html>
Syntax:
21
$('selector expression').before('content');
Page
<!DOCTYPE html>
<html><head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
$('#div1').before('<div style="background-color:yellow"> New div </div>');
});
</script>
<style>
div{
border: 1px solid;
background-color:red;
margin: 2px 0 2px 0;
}
</style>
</head>
<body>
<h1>Demo: jQuery before() method </h1>
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
</body> </html>
22
Page
Syntax:
$('selector expression').append('content');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
$('p').append('World!');
});
</script>
</head>
<body>
<h1>Demo: jQuery append() method </h1>
<p>Hello </p>
</body>
</html>
23
Page
Syntax:
$('selector expression').prepend('content');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
});
</script>
</head>
<body>
<h1>Demo: jQuery prepend() method </h1>
<div>
<label>This is div.</label>
</div>
</body>
</html>
24
Page
Syntax:
$('selector expression').remove();
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
$('label').remove();
});
</script>
</head>
<body>
<h1>Demo: jQuery remove() method </h1>
<div>This is div.
<label>This is label.</label>
</div>
</body>
</html>
25
Page
Syntax:
$('content string').replaceAll('selector expression');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
</script>
<script>
$(document).ready(function () {
$('<span>This is span</span>').replaceAll('p');
});
</script>
</head>
<body>
<h1>Demo: jQuery replaceAll() method </h1>
<div>
<p>This is paragraph.</p>
</div>
Syntax:
$('selector expression').wrap('content string');
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function () {
$('span').wrap('<p></p>');
});
</script>
</head>
<body>
<h1>Demo: jQuery wrap() method </h1>
<div>
<span>This is span.</span>
</div>
<span>This is span.</span>
</body>
</html>
27
Page
1. Display Methods
With jQuery, you can hide and show HTML elements with
the hide() and show() methods:
<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
28
$("p").hide();
Page
});
<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
});
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
29
Page
</body> </html>
You can also toggle between hiding and showing an element with the toggle() method.
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
</body>
</html>
30
Page
Syntax:
$(selector).fadeIn(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after the fading completes.
The following example demonstrates the fadeIn() method with different parameters:
<!DOCTYPE html>
<html> <head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
31
</script>
Page
</head>
</body>
</html>
Syntax:
$(selector).fadeOut(speed,callback);
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.
Page
If the elements are faded in, fadeToggle() will fade them out.
Syntax:
$(selector).fadeToggle(speed,callback);
Example:
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).
Syntax:
$(selector).fadeTo(speed,opacity,callback);
Example:
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
});
33
Page
• slideDown()
• slideUp()
• slideToggle()
Syntax:
$(selector).slideDown(speed,callback);
Example:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
34
padding: 5px;
Page
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
</body>
</html>
35
Page
Syntax:
$(selector).slideUp(speed,callback);
Example:
$("#flip").click(function(){
$("#panel").slideUp();
});
If the elements have been slid down, slideToggle() will slide them up.
If the elements have been slid up, slideToggle() will slide them down.
$(selector).slideToggle(speed,callback);
$("#flip").click(function(){
$("#panel").slideToggle();
});
36
Page
Syntax:
$(selector).animate({params},speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The following example demonstrates a simple use of the animate() method; it moves a
<div> element to the right, until it has reached a left property of 250px:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '250px'});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
37
Page
<div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>
</body>
</html>
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
It is also possible to define relative values (the value is then relative to the element's
current value). This is done by putting += or -= in front of the value:
$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
38
});
Page
You can even specify a property's animation value as "show", "hide", or "toggle":
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
});
39
Page
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.
• 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>
40
• The <b> element is a child of the right <li> and a descendant of <ul> and <div>
Page
With jQuery you can traverse up the DOM tree to find ancestors of an element.
An ancestor is a parent, grandparent, great-grandparent, and so on.
Traversing Up the DOM Tree
Three useful jQuery methods for traversing up the DOM tree are:
• parent()
• parents()
• parentsUntil()
The parent() method returns the direct parent element of the selected element.
This method only traverse a single level up the DOM tree.
The following example returns the direct parent element of each <span> elements:
<!DOCTYPE html>
<html>
<head>
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("span").parent().css({"color": "red", "border": "2px solid red"});
});
</script>
41
</head>
Page
42
Page
The parents() method returns all ancestor elements of the selected element, all the way
up to the document's root element (<html>).
The following example returns all ancestors of all <span> elements:
$(document).ready(function(){
$("span").parents();
});
The parentsUntil() method returns all ancestor elements between two given arguments.
The following example returns all ancestor elements between a <span> and
a <div> element:
$(document).ready(function(){
$("span").parentsUntil("div");
}); 43
Page
With jQuery you can traverse down the DOM tree to find descendants of an element.
A descendant is a child, grandchild, great-grandchild, and so on.
Traversing Down the DOM Tree
Two useful jQuery methods for traversing down the DOM tree are:
• children()
• find()
The children() method returns all direct children of the selected element.
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:
<!DOCTYPE html>
<html> <head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
44
$(document).ready(function(){
Page
</body>
</html>
45
Page
The find() method returns descendant elements of the selected element, all the way
down to the last descendant.
The following example returns all <span> elements that are descendants of <div>:
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("div").find("span").css({"color": "red", "border": "2px solid red"});
});
</script>
</head>
<body>
<p>p (child)
Page
<span>span (grandchild)</span>
</body>
</html>
With jQuery you can traverse sideways in the DOM tree to find siblings of an element.
There are many useful jQuery methods for traversing sideways in the DOM tree:
• siblings()
• next()
• nextAll()
• nextUntil()
• prev()
47
• prevAll()
Page
• prevUntil()
The siblings() method returns all sibling elements of the selected element.
<!DOCTYPE html>
<html> <head>
<style>
.siblings * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("h2").siblings().css({"color": "red", "border": "2px solid red"});
});
</script>
</head>
<body class="siblings">
<div>div (parent)
<p>p</p>
<span>span</span>
<h2>h2</h2>
<h3>h3</h3>
<p>p</p>
48
</div>
Page
</body> </html>
The next() method returns the next sibling element of the selected element.
$(document).ready(function(){
$("h2").next();
});
49
Page
The nextAll() method returns all next sibling elements of the selected element.
$(document).ready(function(){
$("h2").nextAll();
});
jQuery nextUntil() Method
The nextUntil() method returns all next sibling elements between two given arguments.
The following example returns all sibling elements between a <h2> and a <h6> element:
$(document).ready(function(){
$("h2").nextUntil("h6");
});
The prev(), prevAll() and prevUntil() methods work just like the methods above but
50
The most basic filtering methods are first(), last() and eq(), which allow you to select a
specific element based on its position in a group of elements.
Other filtering methods, like filter() and not() allow you to select elements that match,
or do not match, a certain criteria.
The first() method returns the first element of the specified elements.
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></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>
51
</body>
</html>
52
Page
The last() method returns the last element of the specified elements.
$(document).ready(function(){
$("div").last();
});
The eq() method returns an element with a specific index number of the selected
elements.
The index numbers start at 0, so the first element will have the index number 0 and not
1. The following example selects the second <p> element (index number 1):
$(document).ready(function(){
$("p").eq(1);
53
});
Page
<h1>Welcome to My Homepage</h1>
</body>
</html>
54
Page
The filter() 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.
The following example returns all <p> elements with class name "intro":
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to My Homepage</h1>
</body>
</html>
55
Page
The not() method returns all elements that do not match the criteria.
56
Page