What Is Jquery?
What Is Jquery?
jQuery is a fast and concise JavaScript Library that simplifies HTML document
traversing, event handling, animating, and Ajax interactions for rapid web
development.
jQuery is a library of JavaScript functions.
It takes a lot of common task that require many lines of JavaScript code to
accomplish, and wraps them into methods that can call with a single line of code.
It simplifies complicated things from JavaScript, AJAX calls and DOM
manipulation.
jQuery Features
Here are some of the things you can do with jQuery:
jQuery makes JavaScript programming faster and more efficient
jQuery is open source and has a large user community, meaning it is consistently
supported and added-on to.
Select HTML elements in various ways, easing access to the elements to be
enhanced.
Style the HTML elements dynamically, by manipulating their CSS properties or
classes.
Manipulate the DOM (Document Object Model) of your page
Animate the DOM elements, e.g. fading in / out, sliding, change color, change
size etc.
Respond to events like mouse-over, mouse-out, change etc.
AJAX enable your web pages using jQuery's smart AJAX features.
Use the many jQuery plugins (incl. jQuery UI), which contains all kinds of smart
widgets.
Create advanced web application user interfaces, which resemble desktop user
interfaces.
1. Downloading jQuery
To use jQuery, you need to download the jQuery library (explained below), and
include it on the pages you wish to use it.
The jQuery library is a single JavaScript file, and you reference to it using the HTML
<script> tag:
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
2. jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a
CDN (Content Delivery Network).
To use jQuery from Google, use one of the following:
Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jQuery.min.js"></script>
</head>
jQuery Syntax:-
The jQuery syntax is made for selecting HTML elements and perform some action on
the element(s).
Syntax : $(selector).action()
$ sign to define/access jQuery
(selector) to "query (or find)" HTML elements
jQuery action() to be performed on the element(s)
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".
click()
The click() function attaches an event handler function to an HTML element, which
is executed when the user clicks the HTML element.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("p").click(function () {
$(this).hide();
});
});
</script>
</head>
<body>
<h2>jQuery click event example</h2>
<p>This paragraph will be hidden when you click on it.</p>
</body>
</html>
dblclick()
The dblclick() function works just like the click() function, except it reacts to double
clicks instead of single clicks.
mouseenter()
The mouseenter() function attaches an event handler function to an HTML element,
which is executed when the mouse pointer enters the HTML elements area on the
page.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("#para").mouseenter(function () {
$(this).slideUp();
});
});
</script>
</head>
<body>
<h2>jQuery mouseenter event example</h2>
<p id="para"> This paragraph will be hidden with a slideup effect when mouse pointer
enters on this paragraph. </p>
</body>
</html>
mouseleave()
The mouseleave() function attaches an event handler function to an HTML element,
which is executed when the mouse pointer leaves the HTML elements. It is thus kind
of opposite in function to the mouseenter()
Example:
<script>
$(document).ready(function () {
$("#para").mouseenter(function () {
$(this).slideUp();
});
});
</script>
hover()
The hover() function is executed when the mouse enters the HTML element, and one
that is executed when the mouse leaves the HTML element.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
p{
padding: 20px;
font: 20px sans-serif;
background: #f2f2f2;
}
p.highlight {
background: yellow;
}
</style>
<script>
$(document).ready(function () {
$("p").hover(function () {
$(this).addClass("highlight");
}, function () {
$(this).removeClass("highlight");
});
});
</script>
</head>
<body>
<p>Place mouse pointer on me.</p>
<p>Place mouse pointer on me.</p>
</body>
</html>
keydown()
The keydown() function attaches an event handler to a form field, which is executed
when a key is pressed down, when the form field has focus.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("input").keydown(function () {
$("input").css("background-color", "green");
});
});
</script>
</head>
<body>
<h2>jQuery keydown event example</h2>
Write anything here: <input type="text">
<p>The background color of input text field will change to green, when You press a key
inside the input field.</p>
</body>
</html>
keyup()
The keyup() function attaches an event handler to a form field, which is executed
when a key is released, when the form field has focus. In other words, it works
opposite of keydown().
keypress()
The keypress() function attaches an event handler to a form field, which is executed
when a key is pressed (pressed down and released), when the form field has focus.
change()
The jQuery change() method attach an event handler function to the <input>,
<textarea> and <select> elements that is executed when its value changes.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("textarea").change(function () {
alert("The text in textarea has been changed.");
});
});
</script>
</head>
<body>
<h2>jQuery change event example</h2>
<textarea type="text"></textarea>
<p> Write something in the textarea and then click outside of the textarea box. You will
see an alert message that the value of the textarea has been changed.</p>
</body>
</html>
focus()
The focus() function attaches an event handler function to a form field, which is
executed when the form field gets focus.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("input").focus(function () {
$(this).css("background-color", "#444");
});
});
</script>
</head>
<body>
<h2>jQuery focus event example</h2>
Your Name: <input type="text" name="name"> <br />
Your Age: <input type="text" name="age">
</body>
</html>
blur()
The blur() function attaches an event handler function to a form field, which is
executed when the form field loses focus. In other words, the opposite of
the focus() function.
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("input").blur(function () {
$(this).css("background-color", "#FF0000");
});
});
</script>
</head>
<body>
<h2>jQuery focus event example</h2>
Your Name: <input type="text" name="name"> <br />
Your Age: <input type="text" name="age">
</body>
</html>
jQuery Effects
JQuery enables you to apply certain standard effects to HTML elements in your page.
Each effect is carried out by calling a special effect function in JQuery.
The most common of these effect functions are:
jQuery show(), hide() and toggle()
You can hide and show HTML elements with the hide() and show().
The JQuery toggle() function toggles the visibility state of an HTML element. In
other words, if the element is hidden, it will be shown. If it is shown, it will be
hidden. Again, you can set the speed of the toggling via a parameter.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("#hide").click(function () {
$("p").hide();
});
$("#show").click(function () {
$("p").show();
});
});
</script>
</head>
<body>
<h2>jQuery show and hide example</h2>
<p>This is example of hide and show.</p>
<button id="hide">hide</button>
<button id="show">show</button>
</body>
</html>
You can pass a speed to the show() and hide() method which tells JQuery how fast to
show the element.
Example:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
p{
padding: 15px;
background: #F0E68C;
}
</style>
<script>
$(document).ready(function () {
// Hide displayed paragraphs with different speeds
$(".hide-btn").click(function () {
$("p.normal").hide();
$("p.fast").hide("fast");
$("p.slow").hide("slow");
$("p.very-fast").hide(50);
$("p.very-slow").hide(2000);
});
fadeTo()
The fadeTo() function enables you to fade an HTML element partially in or out,
making it transparent. You pass parameter between 0 and 1 which tells how much to
fade the element. 0 is fully transparent (hidden), and 1 is fully opaque (shown). You
can set the speed as a parameter to the function.
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("p").fadeTo("slow", 0.25);
$("h2").fadeTo("slow", 0.10);
$("button").fadeTo("fast", 0.80);
});
});
</script>
</head>
<body>
<h2>jQuery fadeTo effect example</h2>
<p>We have adjusted the opacity of different html elements on this page in the script
part,
lets see how they look after the button click event.</p>
<button>FadeTo</button>
</body>
</html>
Assignment No 2 - jQuery
1. Write a program to demonstrate jQuery ID selector with Click event.
2. Write a program to demonstrate jQuery class selector with Click event.
3. Enable or Disable Button based on condition using jQuery
https://www.aspsnippets.com/Articles/Enable-or-Disable-Button-based-on-condi
tion-using-jQuery.aspx
4. Clear TextBox on Focus using jQuery
https://www.aspsnippets.com/Articles/Clear-TextBox-on-Focus-using-jQuery.aspx
5. Set the background color red of the following elements after button click using
jQuery.
6. Toggle (Show Hide) DIV on Button Click using JavaScript and jQuery.
https://www.aspsnippets.com/Articles/Toggle-Show-Hide-DIV-on-Button-Click-using-J
avaScript-and-jQuery.aspx
7. PAN Card Number validation using jQuery and Regular Expression.
https://www.aspsnippets.com/Articles/PAN-Card-Number-validation-using-jQuery-and-
Regular-Expression.aspx
8. Create a paragraph element with some text and append it to the end of the
document body using jQuery.
https://www.aspsnippets.com/Articles/Toggle-Show-Hide-DIV-on-Button-Click-using-J
avaScript-and-jQuery.aspx
9. Using jQuery add the class " w3r_font_color " to the last paragraph element.
<p>PHP</p>
<p>Java</p>
<p>Python</p>
p{
margin: 8px;
font-size: 16px;
}
.w3r_font_color{
color: red;
}
.w3r_background {
background: yellow;
}
10. Scroll to the top of the page with jQuery.
https://www.w3resource.com/jquery-exercises/part1/jquery-practical-exercise-2.php
11. Create a paragraph element with some text and add the class to change text color
red after click on button using jquery.
12. jQuery Form Validation Example in Asp.net
https://www.aspsnippets.com/Articles/jQuery-Form-Validation-Example-in-ASP
Net.aspx
https://www.c-sharpcorner.com/UploadFile/ee01e6/jquery-validation-with-Asp-
Net-web-form/
13. Show Calendar Icon (Image) next to TextBox using jQuery DatePicker
(Calendar)
https://www.aspsnippets.com/Articles/Show-Calendar-Icon-Image-next-to-Text
Box-using-jQuery-DatePicker-Calendar.aspx
14. Set dd-MMM-yyyy Date format in jQuery DatePicker
https://www.aspsnippets.com/Articles/Set-dd-MMM-yyyy-Date-format-in-jQuer
y-DatePicker.aspx