In the previous lecture, we learned:
In this lecture, we will continue learning more useful jQuery functions and then move toward HTML Forms and Form Validation.
Topics covered:
The .hasClass() function checks whether an element contains a specific CSS class.
.hasClass('classname')
$('#box').hasClass('active');
True
or
False
depending on whether the class exists.
Useful for:
Adds a CSS class to an element.
.addClass('classname')
$('#box').addClass('active');
<div id="box">
<div id="box" class="active">
Removes an existing class.
.removeClass('classname')
$('#box').removeClass('active');
Useful when:
Adds a class if it does not exist.
Removes it if it already exists.
.toggleClass('classname')
$('#box').toggleClass('active');
First click:
Add Class
Second click:
Remove Class
Third click:
Add Again
Used to get or set attributes.
$('#box').attr('id');
box
$('#box').attr('title','Welcome');
Commonly used for:
Removes an attribute.
.removeAttr('attribute')
$('#box').removeAttr('title');
The title attribute is removed from the element.
Returns the position of an element.
$('.item').index(this);
0
1
2
3
depending on the position.
Selects an element at a specific position.
.eq(index)
$('li').eq(2);
Selects the third list item.
(Remember indexing starts from 0.)
Checks whether a condition is true.
$('#box').is(':visible');
True
or
False
.is(':checked')
.is(':visible')
.is(':hidden')
Excludes selected elements.
$('div').not('.active');
Select all divs except those having class active.
Used to get or set values of form fields.
$('#name').val();
$('#name').val('Ali');
Textboxes
Dropdowns
Textareas
Removes an element completely.
$('#box').remove();
The element is deleted from the page.
Creates a copy of an element.
$('#box').clone();
Useful for:
Create a button.
When clicked:
Show:
Hello
Show:
Goodbye
Show:
You are not allowed
Students should practice:
Study the following code carefully:
var id = $(this).attr('id');
$('.tab-content').hide();
$('#'+id).show();
Get clicked element ID.
$(this).attr('id');
Hide all tab content.
$('.tab-content').hide();
Show only matching content.
$('#'+id).show();
Build a simple tab system.
Example:
Tab 1
Tab 2
Tab 3
When a tab is clicked, only its content should appear.
Used to create custom animations.
.animate()
$('#box').animate({
left:'200px'
});
$('#box').animate({
width:'300px',
height:'300px'
});
Forms are used to collect information from users.
Examples:
The <form> tag is used to create forms.
It has:
<form>
</form>
Forms collect data and send it to the server.
Examples:
Name
Email
Password
Profile Image
The form method determines how data is sent.
Data is sent through the URL.
Example:
page.php?name=Ali
Data is sent in the request body.
Login Forms
Registration Forms
Contact Forms
Used for normal text input.
Examples:
Name
City
Country
Used for passwords.
Characters are hidden.
Example:
******
Allows users to choose from a list.
Example:
Select Country
Pakistan
India
USA
Canada
Used for large text input.
Examples:
Message
Comments
Address
Used to upload files.
Examples:
Profile Picture
Resume
Documents
Allows multiple selections.
Example:
Skills
☑ HTML
☑ CSS
☐ PHP
☐ jQuery
Allows only one selection.
Example:
Gender
○ Male
○ Female
Only one option can be selected.
Used to submit form data.
Example:
Submit
Resets all form fields.
Example:
Reset
User Fills Form
↓
Clicks Submit
↓
Data Sent To Server
↓
Server Processes Data
↓
Response Returned
Install the following jQuery validation plugin:
jquery.validate.js
The plugin helps:
Create a registration form and test:
using:
jquery.validate.js
In this lecture, we learned:
In the next lecture, we will start practical form handling and learn how JavaScript, jQuery, and PHP work together to process user data.