What is jQuery?
How do you include jQuery in your project?
<script> tag in your HTML, either linking to a CDN (Content Delivery Network) or a local file:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
How do you select elements in jQuery?
$("p") // Selects all <p> elements
$("#id") // Selects an element with a specific id
$(".class") // Selects elements with a specific class
How do you handle events in jQuery?
.click(), .on(), or .hover() to bind events:
$("#button").click(function() {
alert("Button clicked!");
});
How do you manipulate the DOM with jQuery?
.html(), .text(), .append(), and .remove():
$("#element").html("New content");
Explain the difference between .on() and .bind() in jQuery.
.on() is more flexible and recommended for handling events, allowing delegation and handling events for dynamically added elements. .bind() is deprecated but was used for binding events to elements.What is event delegation in jQuery?
$("#parent").on("click", ".child", function() {
alert("Child clicked!");
});
How can you perform animations with jQuery?
.fadeIn(), .fadeOut(), .slideUp(), and .slideDown():
$("#element").fadeIn(1000); // Fades in the element over 1 second
What are jQuery's utility functions?
$.each(), $.extend(), and $.ajax() are utilities provided by jQuery:
$.each(array, function(index, value) {
console.log(index, value);
});
Explain the concept of "chaining" in jQuery.
$("#element").css("color", "red").slideUp(2000).slideDown(2000);
How does jQuery handle asynchronous operations?
.ajax() method for asynchronous requests, supporting various settings like url, type, data, and success callbacks:
$.ajax({
url: "data.json",
method: "GET",
success: function(data) {
console.log(data);
}
});
What are jQuery plugins, and how do you create one?
(function($) {
$.fn.myPlugin = function() {
return this.each(function() {
// Plugin code here
});
};
})(jQuery);
What is the purpose of $.Deferred() and how is it used?
$.Deferred() is used for managing asynchronous operations, providing a way to handle success, failure, and progress of async tasks:
var deferred = $.Deferred();
deferred.done(function() {
console.log("Completed!");
});
deferred.resolve();
Explain how jQuery's $.when() function works.
$.when() is used to handle multiple deferred objects, ensuring that all of them are completed before executing a callback:
$.when($.ajax("url1"), $.ajax("url2")).done(function(response1, response2) {
console.log("Both requests complete");
});
How does jQuery handle browser compatibility issues?