CSC329 ยท 2082 ยท 10 marks
What are jQuery selectors? Explain jQuery callback and chaining effects with appropriate jQuery scripts.
What are jQuery selectors? Explain jQuery callback and chaining effects with appropriate jQuery scripts.[10]
Quick answer
jQuery selectors allow us to select and manipulate HTML elements. They are used to "find" (or select) HTML elements based on their name, id, class, attributes, and more.
jQuery Selectors, Callback, and Chaining Effects
1. jQuery Selectors
All selectors in jQuery start with the dollar sign and parentheses:
$()
Types of jQuery Selectors
| Selector | Description | Example |
|---|---|---|
| Element Selector | Selects elements based on element name | $("p") selects all <p> elements |
| ID Selector | Selects a single element with a specific id | $("#myId") |
| Class Selector | Selects all elements with a specific class | $(".myClass") |
| Universal Selector | Selects all elements | $("*") |
| this Selector | Refers to the current HTML element | $(this) |
Example of Element Selector
$(document).ready(function() {
$("button").click(function() {
$("p").hide();
});
});
When a user clicks the button, all <p> elements will be hidden.
2. jQuery Callback
A callback function is a function that is executed after the current effect is fully finished. This is important because jQuery effects take time (e.g., slow, fast, milliseconds), and without a callback, the next line of code may run before the effect completes.
Syntax
$(selector).hide(speed, callback);
speed- optional; can be"slow","fast", or millisecondscallback- optional; the function to execute after the effect completes
Example of Callback
$("button").click(function() {
$("p").hide("slow", function() {
alert("The paragraph is now hidden");
});
});
Explanation:
- When the button is clicked, the
<p>element hides slowly. - Only after the hide effect is completely finished, the
alert()callback function executes. - Without the callback, the alert might fire before the hiding animation completes.
Another Callback Example with fadeIn
$("button").click(function() {
$("p").fadeIn(1000, function() {
alert("Fade In complete!");
});
});
3. jQuery Chaining
Chaining allows us to run multiple jQuery methods/commands one after another on the same element(s) in a single statement. This makes code shorter and more efficient.
To chain an action, we simply append the action to the previous action.
Syntax
$(selector).method1().method2().method3();
Example of Chaining
$("button").click(function() {
$("#div").css("color", "red").slideUp(200).slideDown(200);
});
Explanation of the above example:
css("color", "red")- changes the text color of#divto red.slideUp(200)- slides the element up in 200 milliseconds.slideDown(200)- slides the element back down in 200 milliseconds
All three methods are applied to the same element #div one after another.
Another Chaining Example
$(document).ready(function() {
$("button").click(function() {
$("p")
.css("color", "blue")
.fadeOut(1000)
.fadeIn(1000);
});
});
Explanation:
- The
<p>element first turns blue. - Then it fades out over 1 second.
- Then it fades back in over 1 second.
Summary Table
| Feature | Description | Key Benefit |
|---|---|---|
| Selectors | Find/select HTML elements by name, id, class | Easy DOM access |
| Callback | Function executed after an effect finishes | Ensures sequential execution |
| Chaining | Multiple methods applied to same element in one line | Shorter, cleaner code |
Note: jQuery follows the syntax
$(selector).action()where$grants access to jQuery,selectorfinds HTML elements, andaction()performs operations on them.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.