Web Application Development: Jquery
Web Application Development: Jquery
jQuery
Outline
• Concepts of jQuery
• jQuery syntax
• jQuery events
• jQuery effects
• jQuery DOM manipulation
• jQuery AJAX
2
Introduction
• jQuery is a lightweight JavaScript library
3
Introduction
The main uses of jQuery on the websites
include:
– HTML/DOM manipulation
– CSS manipulation
– HTML event methods
– Effects and animations
– AJAX methods
4
Installing jQuery
• jQuery can be added to the webpages using off-
line method or on-line method
<head>
<script src="jquery-1.12.4.min.js">
</script>
</head>
5
Installing jQuery
• In on-line method, jQuery is included from a
Content Delivery Network (CDN). Both Google and
Microsoft host jQuery
• Include jQuery from Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/
1.12.4/jquery.min.js"></script>
</head>
• Include jQuery from Microsoft CDN:
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/
jquery-1.12.4.min.js"></script>
</head>
6
jQuery Syntax
• jQuery syntax allows to select HTML
elements and perform actions on these
elements
• Basic syntax is:
$(selector).action()
• $ sign allows to define/access jQuery. It
is a shortcut for jQuery
• selector allows to find/query HTML
elements
• action() allows to perform some actions
on these HTML elements
7
Examples of jQuery Syntax
8
jQuery Selectors
9
Element Selector
In jQuery, element selector selects elements
based on element name
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
<html>
…
<p>Hello</p>
<button>Click me</button>
…
</html>
10
ID Selector
In jQuery, id selector selects elements based
on id attribute
$(document).ready(function(){
$("button").click(function(){
$("#p1").hide();
});
}); <html>
…
<p>jQuery Introduction</p>
<p id=“p1”>Hello</p>
<button>Click me</button>
…
</html>
11
Class Selector
In jQuery, class selector selects elements
based on class name
$(document).ready(function(){
$("button").click(function(){
$(".intro").hide();
});
}); <html>
…
<p class=“intro”>jQuery Introduction</p>
<p class=“intro”>Hello</p>
<button>Click me</button>
…
</html>
12
Popular jQuery Selectors
Syntax Description
$("ul li:first") Selects the first <li> element of the first <ul>
13
Popular jQuery Selectors
Syntax Description
14
jQuery Events
• Event syntax:
$(“selector”).jQueryEventMethod()
15
Example of jQuery Events
$(document).ready(function(){
$("button").click(function(){
alert("Hello ICT5 Bachelor Class!");
});
});
<html>
…
<p class=“intro”>jQuery Introduction</p>
<p class=“intro”>Hello</p>
<button>Click me</button>
…
</html>
16
Popular Events
Document/
Mouse Events Keyboard Events Form Events
Window Events
17
jQuery Effects
18
Example of jQuery Effects
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '100px'
});
});
});
<html>
…
<button>Start Animation</button>
<div style=“background:green;height:100px;width:
100px;position:absolute;"></div>
…
</html>
19
Example of jQuery Effects
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '100px'
});
});
});
Before calling animate() After calling animate()
20
jQuery DOM Manipulation
21
Basic DOM Manipulation Functions
• Functions to get web content:
- text(): gets the text content of selected
elements
- html(): gets the content with HTML tag of
selected elements
- val(): gets the value of form fields
- attr(“attribute_name”): gets attribute
values of selected elements
22
Basic DOM Manipulation Functions
• Functions to get web content:
$(document).ready(function(){
$("button").click(function(){
alert("Value: " + $("#p1").text());
});
});
<html>
…
<p>jQuery Introduction</p>
<p id=“p1”>Hello</p>
<button>Click me</button>
…
</html>
23
Basic DOM Manipulation Functions
• Functions to set web content:
- text(“set_value”): sets the text content of
selected elements
- html(“set_value”): sets the content with
HTML tag of selected elements
- val(“set_value”): sets the value of form
fields
- attr(“attribute_name”,“set_value”): sets
attribute values of selected elements
24
Basic DOM manipulation functions
• Functions to set web content:
$(document).ready(function(){
$("button").click(function(){
$(“#p1”).text(“Hello Web Class");
});
});
<html>
…
<p>jQuery Introduction</p>
<p id=“p1”></p>
<button>Click me</button>
…
</html>
25
jQuery AJAX
26
What is AJAX?
• AJAX = Asynchronous JavaScript and XML
• AJAX is a technique for creating fast and dynamic
web pages
• AJAX allows web pages to be updated
asynchronously by exchanging small amounts of
data with the server behind the scenes This means
that it is possible to update parts of a web page,
without reloading the whole page
• Examples of applications using AJAX: Google Maps,
Gmail, Youtube, and Facebook
27
What is AJAX?
28
jQuery AJAX
• jQuery AJAX methods allow to request text,
HTML, XML, or JSON data from a remote
server using both HTTP Get and HTTP Post
requests
• External data can also be loaded directly
into the selected HTML elements of the web
page
29
jQuery load() Method
30
jQuery load() method
Having a “data.txt” file with the following
contents:
31
jQuery load() method
Use load() method to load data from
“data.txt” file:
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("data.txt");
});
});
<html>
…
<div id=“div1"></div>
<button>Load data file</button>
…
</html>
32
jQuery load() method
• Use load() method to load data from
“data.txt” file:
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("data.txt");
});
});
• Loaded Results:
Welcome to the class about jQuery!
jQuery is a very powerful JavaScript library
33
jQuery GET and POST methods
• Two commonly used methods for a request-
response between a client and server are:
GET and POST.
- GET - Requests data from a specified resource
- POST - Submits data to be processed to a
specified resource
• Syntax:
- $.get(URL,callback);
- $.post(URL,data,callback);
34
jQuery $.get() method
Having an Active Server Page (get_test.asp)
file as follows:
<%
response.write("Welcome to the class
about get and post methods using jQuery")
%>
35
jQuery $.get() method
• Use $.get() method to retrieve data from
“get_test.asp” file on the server:
$(document).ready(function(){
$("button").click(function(){
$.get(“get_test.asp”, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
}); });
});
<html>
…
<button>Get content of asp file</button>
…
</html>
36
jQuery $.get() method
• Use $.get() method to retrieve data from
“get_test.asp” file on the server:
$(document).ready(function(){
$("button").click(function(){
$.get(“get_test.asp”, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
}); });
});
• Response Results:
Data: Welcome to the class about get and post methods
using jQuery
Status: success
37
jQuery $.post() method
Having an Active Server Page (post_test.asp)
file as follows:
<%
dim name
name=Request.Form("name")
Response.Write("Hello " & name)
%>
38
jQuery $.post() method
Use $.post() method to send some data a
long with the request to server:
$(document).ready(function(){
$("button").click(function(){
$.post(“post_test.asp",
{
name: “John Smitt"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
}); });
});
<body>
<button>Post content of asp file</button>
</body>
39
jQuery $.post() method
• Use $.post() method to send some data a
long with the request to server:
$(document).ready(function(){
$("button").click(function(){
$.post(“post_test.asp",
{
name: “John Smitt"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
}); });
});
• Response Results:
Data: Hello John Smitt
Status: success
40
41