Introduction To Jquery: Study Guide For Module No. 6
Introduction To Jquery: Study Guide For Module No. 6
0 10-July-2020
Introduction to jQuery
MODULE OVERVIEW
jQuery is a Javascript library with the philosophy “write less, do more”. It was conceived in 2006
by John Resig. The contents of the module include embedding juery scripts in a web page, its
basic syntax, selectors, event handling and jQuery method effects.
It was originally created by John Resig in early 2006. The jQuery project is currently run
and maintained by a distributed group of developers as an open source project.
<html>
<head>
<script src=“path/jquery-
x.x.js"></script>
$(function() {
// Start here
});
The <script> element – the jQuery code can be placed inside the <script> element.
The $function (handler) – this statement is known as ready event where the handler
is basically a function to be executed safely as the document is ready to be
manipulated.
A jQuery statement starts with the dollar sign ($) and ends with a semicolon (;). The
dollar sign ($) is an alias for jQuery.
Selecting the elements through typical Javascript approach can be tedious but jQuery
can be of great help. The following are the common ways of selecting elements on a
page and do something with them using jQuery.
1. Selecting By Id
$(“#header”)
<body>
<div id=“header”>
<span id=“logo”>Logo
here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
2. Selecting By Class
$(“.updated”)
$(“table”)
4. Combine them
<body>
<div id=“header”>
<span id=“logo”>Logo
here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
6.5 Events
Events are often triggered by the user’s interaction with the web page such moving a
mouse over an element, selecting a radio button, clicking on an element among others.
These are user actions that a web page can respond to. Some of the commonly used
events are listed in the table below.
Table 6.1
Example:
<script>
$(function() {
$(“#message”).click(function(){
alert(“Hello World”);
})
});
</script>
When you click “Click Me” text, this will display alert with the text “Hello World”.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-
3.5.0.js"></script>
<script>
$(function(){
$("p").click(function(){
alert("Hello World");
});
$( "#target2" ).blur(function() {
alert( "Handler for .blur() called." );
});
$( "li" ).hover(
function() {
$( this ).append( $( "<span>***</span>" ) );
}, function() {
$( this ).find( "span").last().remove();
};
$('#target1').keypress(function(e) {
code = e.which;
if(code.toString() == 13) {
alert('Hello, '+$("#target1").val()+' You pressed
enter!');
}
});
});
</script>
</head>
<body>
<p> Click Me</p>
<input id="target1" type="text">
<input id="target2" type="text">
<ul>
<li>Milk</li>
<li>Bread</li>
<li>Chips</li>
<li>Socks</li>
</ul>
</body>
</html>
LEARNING ACTIVITY 1
The following will be submitted on the specified d ate. It includes softcopy of the UI and
codes and the physic al files for the activity.
1. Crete a navigation menu that changes color (use hover event) and when the user
clicks the item, it loads a website to the container. See jQuery load() method.
Item
Item
Item
Item
Effects
With jQuery, you can hide and hsow HTML elements with the hide() and show()
methods.
Syntax:
$(selector).hide(speed, callback);
$(selector).show(speed, callback);
Example:
$("button").click(function(){
$("p").hide(3000,function(){
});
});
Syntax:
$(selector).fadeIn(speed,callback);
$(selector).fadeOut(speed,callback);
$(selector).fadeToggle(speed,callback);
$(selector).fadeTo(speed,callback);
Example:
$("button").click(function(){
$("p").fadeToggle(3000,function(){
With jQuery you can create a sliding effect on elements. It has the following slide
methods:
slideDown()
slideUp()
slideToggle()
Syntax:
$(selector).slideDown(speed,callback);
$(selector).slideUp(speed,callback);
$(selector).slideToggle(speed,callback);
Example:
$("button").click(function(){
$("#flip").slideToggle(3000,function(){
alert("The paragraph is now
shown/hidden");
});
});
LEARNING ACTIVITY 2
The following will be submitted on the specified date. It includes softcopy of the UI and codes
and the physic al files for the activity.
SUMMARY
REFERENCES
Lengstorf, J. and Wald, K (2016). “Pro PHP and JQUERY. 2nd edition. Spring Science and
Business Media: NewYork.
w3schools.com/jquery