JQuery Notes
JQuery Notes
Introduction to jQuery
Overview of jQuery
What is jQuery?
o jQuery is a fast, small, and feature-rich JavaScript library.
o It simplifies tasks like HTML document traversal, event handling, animations,
and AJAX interactions.
o Motto: "Write less, do more."
History
It was created by John Resig and was released at BarCamp NYC in January 2006.
Became one of the most popular JavaScript libraries due to its simplicity and cross-
browser compatibility.
It is currently maintained by a team of developers led by Timmy Willison
Features
Simplified HTML document traversal and manipulation.
Event handling and animation capabilities.
AJAX support for seamless server interactions.
Cross-browser compatibility.
Extensibility via plugins.
Advantages
Easy to use and learn.
Reduces code complexity.
Excellent community support.
Enhances user experience with animations and dynamic content.
Setting up jQuery
Installation Methods
1. Using CDN (Content Delivery Network)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Downloading and Including Locally
Download from jQuery website.
Include using:
<script src="path/to/jquery.min.js"></script>
Understanding the jQuery Syntax
Basic jQuery Syntax
The basic syntax of jQuery follows this pattern:
$(selector).action();
$ → Represents jQuery.
selector → Specifies the HTML element(s) to target.
action() → Defines what happens to the selected elements.
Example:
The following example says: When a click event fires on a <p> element; hide the
current <p> element:
$(document).ready(function() {
$('p').click(function() {
$(this).hide();
});
});
Example
$(document).ready(function() {
$("p").css("color", "blue"); // Selects all <p> elements and changes text color
$(".highlight").css("background-color", "yellow"); // Selects elements with class 'highlight'
$("#mainTitle").css("font-size", "24px"); // Selects the element with ID 'mainTitle'
});
Using Basic Filters in jQuery
Filters allow you to refine selections and target specific elements.
3.1 Common jQuery Filters
Filter Description Example
$("#changeHtml").click(function() {
$("#htmlElement").html("<b>Bold HTML Content</b>");
});
});
2.2 Changing Attributes
.attr(): Gets or sets attribute values.
Example: Changing Image Source
$(document).ready(function() {
$("#changeImage").click(function() {
$("#myImage").attr("src", "new-image.jpg");
});
});
$("#clearList").click(function() {
$("#list").empty();
});
});
3.3 Modifying CSS
.css(): Changes CSS properties dynamically.
Example: Changing Background Color
$(document).ready(function() {
$("#changeColor").click(function() {
$("body").css("background-color", "lightblue");
});
});
Example
Basic Validation Using .val()
The .val() method retrieves or sets values of form inputs.
$(document).ready(function() {
$("#myForm").submit(function(event) {
event.preventDefault();
var name = $("#name").val();
if (name === "") {
alert("Name field cannot be empty");
} else {
alert("Form submitted successfully!");
}
});
});
Checking for Email Format
Using regular expressions (RegEx) to check if an email is valid.
$(document).ready(function() {
$("#myForm").submit(function(event) {
event.preventDefault();
var email = $("#email").val();
var emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;
if (!email.match(emailPattern)) {
alert("Invalid email address");
} else {
alert("Email is valid!");
}
});
});
Password Strength Validation
Checking for password length and complexity.
$(document).ready(function() {
$("#myForm").submit(function(event) {
event.preventDefault();
var password = $("#password").val();
if (password.length < 6) {
alert("Password must be at least 6 characters long");
} else {
alert("Password is strong!");
}
});
});
Using jQuery Plugins for Form Validation
For advanced validation, jQuery plugins like simplify the process. This plugin provides
built-in validation rules and easy customization.
$(document).ready(function(){
$("#myForm").validate({
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please enter your name",
email: "Enter a valid email address"
},
submitHandler: function(form) {
form.submit();
}
});
});
This automatically applies validation rules and displays error messages.
Implementing Custom Form Validation
Apart from using plugins, we can create our own custom validation functions using jQuery.
Example:
Custom Validation for Phone Number
Validating a 10-digit phone number.
Example: Custom Phone Validation
$(document).ready(function() {
$("#myForm").submit(function(event) {
event.preventDefault();
var phone = $("#phone").val();
var phonePattern = /^[0-9]{10}$/;
if (!phone.match(phonePattern)) {
alert("Invalid phone number. Must be 10 digits.");
} else {
alert("Phone number is valid!");
}
});
});
Summary
jQuery simplifies JavaScript by providing easy-to-use methods for DOM
manipulation, event handling, and AJAX interactions.
Effective for enhancing user interfaces and ensuring cross-browser compatibility.