Unit 2
Unit 2
Chapter 2
jQuery Fundamentals
2.4 Exercise
• The following code shows how to include the jQuery library in a Web Page:
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
The src attribute of the <script> specifies the path of the jQuery file.
• 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 who host jQuery
• Following is the way to use Google to host jQuery:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
</head>
Example:
$(“document”).ready(function(){
$("button").click(function(){
$(".test").hide();
$(“#p1”).hide();
});
});
In the above example:
Method Description
blur() Binds a function to blur event of selected elements
focus() Binds a function to the focus event of selected elements
hover() Binds a function to the hover event of selected elements
click() Binds a function to the click event of selected elements
dblclick() Binds a function to the dblclick event of selected elements
change() Binds a function to the change event of selected elements
keypress() Binds a function to the keypress event of selected elements
mouseup() Binds a function to the mouseup event of selected elements
mousedown() Binds a function to the mousedown event of selected elements
on() Binds one or more event handlers to elements and can only be used
once per element.
ready() Binds a function to the ready event of document
Example:
$(“document”).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
In the above example
• $(document).ready() method allows us to bind a function when the document
is fully loaded.
• After document is loaded and when the paragraph <p> is clicked,
The click() method of paragraph is attached and an hide effect is executed.
• The hide() and show() methods are used to hide and show the elements
respectively:
• The toggle() method is used to toggle between the hide() and show() methods.
• Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
$(selector).toggle(speed,callback);
<button id="toggle">Toggle</button>
</body>
</html>
• Syntax:
$(selector).fadeIn(speed,callback);
$(selector).fadeOut(speed,callback);
$(selector).fadeToggle(speed,callback);
$(selector).fadeTo(speed,opacity,callback);
• The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
• The optional opacity parameter in the fadeTo() method specifies fading to a
given opacity (value between 0 and 1).
• The optional callback parameter is a function to be executed after the
function completes.
• Example:
<html>
<head>
<script src="jquery-3.5.1.js"></script>
<script>
$(document).ready(function(){
$("#bt1").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn(1000);
});
$("#bt2").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut(1000);
});
$("#bt3").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle(1000);
});
$("#bt4").click(function(){
$("#div1").fadeTo("slow",0.3);
$("#div2").fadeTo(1000,0.15);
});
});
</script>
<style type="text/css">
#div1{width:80px;
height:80px;
background-color:green;
display:none;
}
#div2{width:80px;
height:80px;
background-color:red;
display:none;
}
</style>
</head>
<body>
<p> Fading effect demonstration on Oxford</p><br>
<button id="bt1">FadeIn</button>
<button id="bt2">FadeOut</button>
<button id="bt3">FadeToggle</button>
<button id="bt4">FadeTo</button><br><br><br>
<div id="div1"></div><br><br>
<div id="div2"></div>
</body>
</html>
});
</script>
<style>
#flip { background-color:grey;
color:white;
text-align:center;
border:solid 5px yellow;
padding:5px}
#panel{background-color:grey;
color:white;
text-align:left;
border:solid 5px yellow;
padding:100 px;
display:none;}
</style>
</head>
<body>
<h1 align="center"> Demonstrate Sliding Effect</h1>
<div id="flip"> Click to Slide the Panel Down</div>
<div id="panel"> Faculty <br>
Student<br>
Administrator<br>
</div>
</body>
</html>
<p>By default, all HTML elements have a fixed position. To change the
position, first the CSS position property of the element should be set to
either relative, fixed, or absolute!</p>
• But when we use jQuery effects, the next line of code can be run even though
the effect is not finished. This can create errors.
• To prevent such thing a callback function is used.
• A callback function is executed after the current effect is finished completely.
Syntax:
$(selector).hide(speed,callback);
Example:
• The example below has a callback parameter that is a function that will be
executed after the hide effect is completed:
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
• The example below has no callback parameter, and the alert box will be
displayed before the hide effect is completed:
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
• jQuery provides various methods like html(), append, get() and many more to modify
or manipulate the content of HTML element and attributes.
• Next, we will discuss different jQuery methods for the manipulations of content or
attribute values in html.
Method Description
text() sets or gets text of selected elements
val() sets or gets value of a form field
html() sets or gets the content of selected elements (including HTML
markup)
attr() sets or gets attribute values.
});
$("#btn3").click(function(){
alert("Value of textbox is:" + $("#t1").val());
});
$("#btn4").click(function(){
alert($("#a1").attr("href"));
});
});
</script>
</head>
<body>
<p id="p1"> This is some <b> bold </b> text in the <i>paragraph </i>
</p><br>
Enter your city:<input type="text" value="bharuch" id="t1"><br><br><br>
</body>
</html>
<html>
<head>
<script src="jquery-3.5.1.js"></script>
<script>
$("document").ready(function(){
$("#btn1").click(function(){
$("#p1").text("Oxford!");
});
$("#btn2").click(function(){
$("#pt2").html("<b>Oxford!</b>");
});
$("#btn3").click(function(){
$("#t1").val("BHARUCH");
});
$("#btn4").click(function(){
$("a").attr({"href" :"anotherpage.html", "title": "set another
page"});
});
});
</script>
</head>
Prepared By: Meghna Vithlani(Asst. Prof, MKICS)
15 Chapter2- jQuery Fundamentals
<body>
<p id="p1">This is a advance web technology book.</p>
<p id="p2">This is second paragraph.</p><br>
<a href="first.html" title="first_page">Click to check</a>
<p>Input field: <input type="text" id="t1" value="SURAT"></p>
<button id="btn1">Set Text</button>
<button id="btn2">Set HTML</button>
<button id="btn3">Set Value</button>
<button id="btn4">Set HREF Attribute</button>
</body>
</html>
Method Description
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
wrap() wraps specified HTML element(s) around each selected element
<html>
<head>
<script src="jquery-3.5.1.js"></script>
<script>
$("document").ready(function(){
$("#btn1").click(function(){
$("p").append(" <b>Best book</b>.");
});
$("#btn2").click(function(){
$("ol").append("<li>Mobile Development</li>");
});
$("#btn3").click(function(){
$("p").prepend(" <b> Best book </b>.");
});
$("#btn4").click(function(){
$("ol").prepend("<li> Mobile Development </li>");
});
$("#btn5").click(function(){
$("img").before("<b>Text Appears Before Logo</b>");
});
$("#btn6").click(function(){
$("img").after("<i>Text Appears After Logo</i>");
Prepared By: Meghna Vithlani(Asst. Prof, MKICS)
16 Chapter2- jQuery Fundamentals
});
$("#btn7").click(function(){
$("p").wrap("<div></div>");
});
});
</script>
<style type="text/css">
div{background-color: orange; border : black 2pt solid}
</style>
</head>
<body>
<p>This is an advanced web technology book.</p>
<p>It is published by Oxford publisher.</p>
<ol>
<li>Web Design1</li>
<li>Java</li>
<li>.Net</li>
</ol>
<img src="Oxford logo.jpg" width="100" height="140"><br>
Method Description
empty() Removes the child elements from the selected element
remove() removes the selected element(s) and its child elements.
unwrap() removes the parent element of the selected elements
<html>
<head>
<script src="jquery-3.5.1.js"></script>
<script>
$("document").ready(function(){
Prepared By: Meghna Vithlani(Asst. Prof, MKICS)
17 Chapter2- jQuery Fundamentals
$("#btn1").click(function(){
$("#div1").empty();
});
$("#btn2").click(function(){
$("#div1").remove();
});
$("#btn3").click(function(){
$("p").unwrap();
});
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:300px;border:solid red 5px ;
background-color:yellow;">
This is some text in the div.
<p>This is first paragraph in the div.</p>
<p>This is another paragraph in the div.</p>
</div>
<br>
<p><b>This paragraph is out of div</b></p>
The following example will return the background-color value of the FIRST matched
element:
<html>
<head>
<script src="jquery-2.1.4.js"></script>
<script>
$("document").ready(function(){
$("#btn1").click(function()
{
alert($("h1").css("background-color"));
});
});
</script>
</head>
<body>
<h1 style="background-color:green;">Advanced Web Technologies</h1>
<p> Welcome to Oxford</p>
<button id="btn1"> Get CSS Property</button>
</body>
</html>
css("propertyname","value");
The following example will set the background-color value for ALL matched elements:
<html>
<head>
<script src="jquery-3.5.1.js"></script>
<script>
$("document").ready(function(){
$("#btn1").click(function()
{
$("h1,p").css("background-color", "yellow");
});
});
</script>
</head>
<body>
<h1> Advanced Web Technologies </h1>
<p> Welcome to Oxford </p>
<button id="btn1"> Set CSS Property</button>
</body>
</html>
The following example will set a background-color and a font-size for ALL matched
elements:
2.4 Exercise
1. Which of the following jQuery method is used to stop jQuery for few
milliseconds?
A. stop() method
B. delay() method
C. slowdown() method
D. pause() method
Ans: A
2. Which jQuery method is used to set one or more style properties to the
selected element?
A. the % sign
B. the & sign
C. the $ sign
D. the @ sign
Ans: C
4. Which tag is used to import the jQuery library file in the code?
A. <head>
B. <script>
C. <import>
D. <style>
Ans: B
5. Which selector expression will hide all the elements having class cute?
A. $(.cute).hide();
B. $(“.cute”).hide();
C. $(“#cute”).hide();
D. $(“.cute”).hides();
Ans: B
A. fadeIn()
B. fadeOut()
Prepared By: Meghna Vithlani(Asst. Prof, MKICS)
20 Chapter2- jQuery Fundamentals
C. show()
D. fadeTo()
Ans: C
7. Which of the following jQuery method is used to sets or gets the value
of form control?
A. html()
B. text()
C. value()
D. val()
Ans: D
A. click()
B. onClick()
C. blur()
D. mouseleave()
Ans: B
10. Which of the following selector expression will select the element
having id “abcd”?
A. $(# abcd)
B. $(‘#abcd’)
C. $(‘.abcd’)
D. $(abcd)
Ans: B
11. Which of the following method does not provides sliding effect?
A. slideUp()
B. slideDown()
C. fadeIn()
D. slideToggle()
Ans: C
12. I have a dropdown menu in my form having class property “city”. What
line of jQuery code should I write if I want to get the value of that
Prepared By: Meghna Vithlani(Asst. Prof, MKICS)
21 Chapter2- jQuery Fundamentals
A. $vcity = $(‘.city’).val();
B. var vcity = $(‘.city’).val();
C. var vcity = $(‘.city’).text();
D. var vcity = $(‘.city’).html();
Ans: B
4. What are the types of selectors in jQuery? Explain each with example