Jquery Notes
Jquery Notes
● HTML/DOM manipulation
● CSS manipulation
● HTML event methods
● Effects and animations
● AJAX
● Utilities
The jQuery library provides several powerful features for web development. It
allows HTML/DOM manipulation, enabling developers to easily change, add, or
remove elements dynamically. With CSS manipulation, jQuery can modify styles
like colors, fonts, and visibility. HTML event methods help in handling user
interactions such as clicks, keypresses, and mouse movements. The effects and
animations feature includes built-in methods for showing, hiding, fading, and
sliding elements. AJAX support allows fetching or sending data to servers
asynchronously without reloading the page. Additionally, utility functions like
$.each() for iteration and $.trim() for trimming whitespace simplify coding
tasks.
Before we execute any jQuery statement, we would like to wait for the
document to be fully loaded. This is because jQuery works on DOM and if
complete DOM is not available before executing jQuery statements, then we will
not get desired result.
Document Representation:
The DOM represents the document as a tree structure, with each element (like
<div>, <p>, <button>, etc.) being a node in the tree. For example:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.7.1.js"></script>
</head>
<script>
$(document).ready(function(){
alert("welcome")
});
</script>
<body>
</body>
</html>
JavaScript statements are executed line by line. However, with effects, the next
line of code can be run even though the effect is not finished. This can create
errors.
The example below has no callback parameter, and the alert box will be
displayed before the hide effect is completed:
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
});
</script>
</head>
<body>
<button>Hide</button>
</body>
jQuery Callback Functions
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
});
</script>
</head>
<body>
<button>Hide</button>
jQuery - Chaining
With jQuery, you can chain together actions/methods.
jQuery Method Chaining
However, there is a technique called chaining, that allows us to run multiple
jQuery commands, one after the other, on the same element(s).
The following example chains together the css(), slideUp(), and slideDown()
methods. The "p1" element first changes to red, then it slides up, and then it
slides down:
Tip: This way, browsers do not have to find the same element(s) more than
once.
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
});
});
</script>
</head>
<body>
<button>Click me</button>
Jquery noConflict()
The noConflict() method releases the hold on the $ shortcut identifier, so that
other scripts can use it.
<script>
$.noConflict();
jQuery(document).ready(function(){
jQuery("button").click(function(){
jQuery("p").text("jQuery is still working!");
});
});
</script>
<body>
<p>This is a paragraph.</p>
<button>Test jQuery</button>
</body>
If you have a block of jQuery code which uses the $ shortcut and you do not
want to change it all, you can pass the $ sign in as a parameter to the ready
method.
<script>
$.noConflict();
jQuery(document).ready(function($){
$("button").click(function(){
$("p").text("jQuery is still working!");
});
});
</script>
Animation
Index1.html
Index3.html
height: '+=150px' – Increases the height of the <div> by 150 pixels from its
current height.
width: '+=150px' – Increases the width of the <div> by 150 pixels from its
current width.
Since all these properties are inside the same animate() function, they occur
simultaneously, creating a smooth transformation where the <div> moves right
while expanding in size.
Font Animation
The example below first moves the <div> element to the right, and then
increases the font size of the text:
Index4.html
The given jQuery code adds an animation effect to a <div> element when a
<button> is clicked. Here’s how it works: