Chapter 7 JQuery (2)
Chapter 7 JQuery (2)
JQUERY
Adapted from w3schools
Prepared by: Mohammed Jabari
2
What it is?
• A JavaScript library
• Greatly simplifies writing JavaScript programs
3
<head>
<script src="jquery-3.2.1.min.js"></script>
</head>
5
jQuery Syntax
jQuery Syntax
• Examples:
$(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".
8
});
jQuery Selectors
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
10
jQuery Selectors
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
11
jQuery Selectors
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
12
$("p").click(function(){
// action goes here!!
});
14
• Example: dblclick()
$("p").dblclick(function(){
$(this).hide();
});
15
• Example: mouseenter()
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
16
• Example: mouseleave()
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
17
• Example: mousedown()
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
18
• Example: 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:
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
19
jQuery Effects
• jQuery hide() and show()
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
20
jQuery Effects
• Hide and show with parameters
$("button").click(function(){
$("p").hide(1000);
});
21
jQuery Effects
• jQuery toggle()
With jQuery, you can toggle between the hide() and show()
methods with the toggle() method.
$("button").click(function(){
$("p").toggle();
});
22
css("propertyname","value");
Example:
$(”#button1").click(function(){
$(“h1”).css("background-color", ”#ccc");
});
23
css({"propertyname":"value","propertyname":"value",...});
Example: