1
What is jQuery? Explain its advantages in web development.
ans.
jQuery is a fast, small, and feature-rich JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. Its advantages include:
- Cross-browser compatibility: Helps write code that works consistently across different browsers.
- Simplified DOM manipulation: Provides easy ways to manipulate HTML elements and attributes.
- AJAX support: Simplifies asynchronous HTTP requests and responses.
- Animation and effects: Provides methods to create animations and add visual effects easily.
2
How do you select elements using jQuery?
ans.
- Element selector:
$("element")
- Class selector:
$(".class")
- ID selector:
$("#id")
- Attribute selector:
$("[attribute='value']")
- Descendant selector:
$("parent child")
- Multiple selectors:
$("selector1, selector2")
3
Explain the difference between
$(document).ready()
and$(window).load()
in jQuery.ans.
$(document).ready(function() { ... })
: Executes when the DOM is fully loaded and ready to be manipulated, but before images and other external resources are fully loaded.
$(window).load(function() { ... })
: Executes after the entire page, including all images and external resources, is fully loaded.
4
How can you handle events in jQuery?
ans.
Events in jQuery are handled using the
.on()
method or shorthand methods like.click()
,.hover()
, etc.$("#myButton").click(function() { // Handler code here });
5
How do you perform AJAX operations in jQuery?
ans.
jQuery simplifies AJAX operations with methods like
$.ajax()
,$.get()
,$.post()
$.ajax({ url: "example.php", type: "GET", data: { param1: "value1", param2: "value2" }, success: function(response) { // Handle successful response }, error: function(xhr, status, error) { // Handle error } });