JQuery Unit-1
JQuery Unit-1
Unit-1
1.1 jQuery Introduction
• jQuery is a lightweight, fast, and feature-rich JavaScript library
designed to client-side scripting. It helps you to manipulate HTML
elements, handle events, make AJAX requests, animate elements,
and more, all with fewer lines of code compared to writing JavaScript.
Key Features
• Cross-browser compatibility: jQuery takes care of browser inconsistencies,
so you don’t have to write separate code for each browser.
• DOM Manipulation: Allows you to easily access and modify HTML
elements.
• Event Handling: Provides simple methods to handle events like mouse
clicks, keypresses, etc.
• Animation: Simplifies animations like hiding/showing elements, sliding
up/down, etc.
• AJAX Support: Makes it easier to interact with server-side data
asynchronously.
1.2 Install and Use jQuery
Library
• Downloading the jQuery File: Download the file from the official jQuery
website and save it in your project folder.
• Using a CDN (Content Delivery Network): This is the preferred method
because it reduces the load on your server. It’s quicker because the library
might already be cached in the user's browser.
• Example: To include jQuery from a CDN, add the following <script> tag in
your HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
• This <script> tag should be placed either in the <head> or before the
closing </body> tag to ensure jQuery is loaded before your code runs.
1.3 Un-Obstructive JavaScript
Example:
$(".example").hide();
// Hides all elements with the class "example“
Explanation:
• $(".example"): Selects all elements with the class example.
• .hide(): Hides the selected elements.
Syntax of jQuery Selectors
• Element Selector: Selects all elements of a specified type.
$('p') // Selects all <p> elements
• Class Selector: Selects all elements with a specific class.
$('.className') // Selects all elements with class 'className'
• ID Selector: Selects an element with a specific ID.
$('#Id') // Selects the element with ID 'Id'
• Attribute Selector: Selects elements with a specific attribute.
$('input[type="text"]') // Selects all <input> elements with type 'text'
Syntax of jQuery Methods
• jQuery provides a wide range of methods for manipulating HTML elements, handling
events, and performing animations.
1. Manipulating HTML/Text Content
• .html(): Sets the HTML content of selected elements.
• // Sets HTML Content
• $('#element').html('New Content');