Lecture 9ABC JQuery
Lecture 9ABC JQuery
Lecture-9ABC
Outline
• jQuery
• Downloading and Implementing JQuery
• JQuery Selectors
• JQuery Chain Methods
• JQuery Events
• JQuery Effects
• CSS Implementation
• Callback
• Image Slider
• Traversing
3
jQuery
• A JavaScript library which greatly simplifies JavaScript programming
• Features
• HTML/DOM manipulation
• CSS manipulation
• HTML event methods
• Effects and animations
• AJAX and other Utilities
4
Installing jQuery
• Download
• http://jquery.com
• For development, use uncompressed file
• E.g., jquery.js , jquery-3.4.1.js
• For deployment, use compressed file
• E.g., jquery-3.4.1.min.js
• For easier upgrades without changing HTML files
• Rename file to jquery.js (or possibly jquery-3.4.1.js)
• Installation
• Load jquery.js before any JS files that use jQuery
• Online API and tutorials
• http://docs.jquery.com
5
HTML DOM
• A standard way for accessing and manipulating HTML
documents.
• Presents an HTML document as a tree-structure
8
DOM?
• Long story short, the DOM is your html document code.
From the
• <!DOCTYPE> to the </html>
Loading jQuery
• In order to use jQuery you need to load it
jQuery CDN
• If you don't want to download and host jQuery yourself, you
can include it from a CDN (Content Delivery Network).
JQuery Example
• Link to run the code
• General Links
12
Wait!!!
• In order to make sure that jQuery can find the element you asked it for,
your browser needs to have loaded it (the DOM needs to be ready)
$(document).ready(function(){
// insert jQuery code here…
});
• Shorthand:
$(function(){
// your jQuery code
});
13
jQuery Syntax
• Tailor made for selecting HTML elements and performing
some action on the element(s)
• With jQuery you select (query) HTML elements and perform "actions" on
them.
• $(selector).action()
• $ sign to define/access jQuery
.addClass("isCool
$ ("p")
");
id="foo class="
<p> <div>
" foo"
api.jquery.com/category/selectors/
17
• Before:
<p>
• After:
<p class="sophisticated">
18
• Before:
<p class="sophisticated">
• After:
<p class="">
19
$("div").show();
• Before:
<div style="display:none;">
• After:
<div style="display:block;">
20
• Before:
<p id="eric">Is Lame</p>
• After:
<p id="eric">Is Cool</p>
21
JQuery Selectors…
• $(this).hide() - hides the current element.
• $("p").hide() - hides all <p> elements.
• $(".test").hide() - hides all elements with
class="test".
• $("#test").hide() -hides the element with
id="test".
Examples
22
Before:
<p style="display:none;">I’m a para</p>
After:
<p class=“sophisticated”
style="display:block;">
Hello World!
</p>
23
$("#foo").text();
$("#foo").text("foo");
25
myVar = $ • Output:
("p").text(); myVar === "Eric"
26
myVar = "BoBeric";
$("p").text(myVar); • <p>BoBeric</p>
27
Basic Selectors
28
Activity
Use the correct selector to hide the element with id="test".
29
Activity
Use the correct selector to hide all elements with class="test".
30
Activity
Use the correct selector to hide all elements in the document.
31
Hierarchical Selectors
33
More Functions
34
Outline
• jQuery
• Downloading and Implementing JQuery
• JQuery Selectors
• JQuery Chain Methods
• JQuery Events
• JQuery Effects
• CSS Implementation
• Callback
• Image Slider
• Traversing
35
jQuery Event
• jQuery is tailor-made to respond to events in an HTML page.
• All the different visitor's actions that a web page can respond to are
called events.
Event Handlers
• Syntax
• $("selector").event( someHandler );
• Example
$("p").click(function(){
alert(“Don’t click me!!”);
});
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
38
function showAlert() {
alert("I am live");
}
$(function(){
$("#alert-button-1").click(function() {
alert("I am live");
});
});
39
Activity
• When we press a keyboard key inside text field element, it
should be hidden. Use the correct event to do so.
41
Outline
• jQuery
• Downloading and Implementing JQuery
• JQuery Selectors
• JQuery Chain Methods
• JQuery Events
• JQuery Effects
• HTML/CSS Implementation
• Callback
• Image Slider
• Traversing
42
jQuery Effects
jQuery has many effects, i.e. Hide, Show, Toggle, Slide,
Fade, and Animate.
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
• Syntax:
$(selector).fadeIn(speed,callback);
$(selector).fadeTo(speed,opacity,callback);
• Example
44
• Syntax:
$(selector).slideDown(speed,callback);
$(selector).slideUp(speed,callback);
• Example
• https://
www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle
45
$(document).ready(function(){
$("button").click(function(){
var div = $("div");
div.animate({left: '100px'}, "slow");
div.animate({fontSize: '3em'}, "slow");
});
});
Example of animate
46
• Chaining Method
$("button").click(function(){
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
}
48
Outline
• jQuery
• Downloading and Implementing JQuery
• JQuery Selectors
• JQuery Chain Methods
• JQuery Events
• JQuery Effects
• Callback
• HTML/CSS Implementation
• Traversing
• Image Slider (jQuery Image Slider - Quick & Easy)
49
• Example
Add Elements
• append() - Inserts content at the end of the selected elements
• prepend() - Inserts content at the beginning of the selected
elements
• after() - Inserts content after the selected elements
• before() - Inserts content before the selected elements
Remove Elements
• remove() - Removes the selected element (and its child elements)
• empty() - Removes the child elements from the selected element
51
</section>
</body>
53
</section>
</body>
54
<body>
<div id="page">
<h1 id="header"></h1>
<h2>Buy groceries</h2>
<ul>
<li id="one" class="hot"><em>fresh</em> figs</li>
<li id="two" class="hot">pine nuts</li>
<li id="three" class="hot">honey</li>
<li id="four">balsamic vinegar</li>
</ul>
</div>
</body>
55
$('ul').append($listHTML);
});
57
$('ul').append('<p>'+$listText+'</p>');
});
58
$('li').append(
'<i>'+$listItemHTML+'</i>');
});
<body>
<div id="page">
<h1 id="header"></h1>
<h2>Buy groceries</h2>
<ul>
<li id="one" class="hot"><em>fresh</em> figs</li>
<li id="two" class="hot">pine nuts</li>
<li id="three" class="hot">honey</li>
<li id="four">balsamic vinegar</li>
</ul>
</div>
</body>
60
Changing Content
$(function() {
$('li#one').remove();
$
('li:contains("pine")').text('almonds');
$('li.hot').html(function() {
return '<em>' + $(this).text() +
'</em>'; });
});
61
$('li.hot').prepend('+ ');
var $newListItem =
$('<li><em>gluten-free</em>
soy sauce</li>');
$('li:last').after($newListItem);
});
62
Changing CSS
$(function() {
var backgroundColor =
$('li').css('background-color');
$('ul').append(
'<p>Color was: ' + backgroundColor + '</p>');
$('li').css({
'background-color': '#c5a996',
'border': '1px solid #fff',
'color': '#000',
'text-shadow': 'none',
'font-family': 'Georgia',
'padding-left': '+=75'
});
});
64
Basic Effects
$(function() {
var $li = $('li');
$li.hide().each(function(index) {
$(this).delay(700*index)
.fadeIn(700);
});
$li.on('click', function() {
$(this).fadeOut(700);
});
});
65
Traversing
$(function() {
var $h2 = $('h2');
$('ul').hide();
$h2.append(
'<a class="show">show</a>');
$h2.on('click', function() {
$h2.next('ul').fadeIn(500)
.children('.hot')
.addClass('complete');
$h2.find('a').fadeOut();
});
});
66
Traversing CSS
.complete {
background-color: #999;
background-image: url("icon-trash.png");
background-position: right center;
background-repeat: no-repeat;
border-bottom: 1px solid #b0b0b0;
border-top: 1px solid #666;
color: #fff;
text-shadow: 2px 2px 1px #333;
}
69
Outline
• jQuery
• Downloading and Implementing JQuery
• JQuery Selectors
• JQuery Chain Methods
• JQuery Events
• JQuery Effects
• Callback
• HTML/CSS Implementation
• Traversing
• Image Slider (jQuery Image Slider - Quick & Easy)
• Drag and drop Links
70
Further Reading
• http://api.jquery.com
• http://docs.jquery.com
• http://www.w3schools.com/jquery/default.asp