🔹 1. Why Form Handling
with jQuery Is Powerful
Forms are where your app connects to the user’s intent
— whether it's signing up, logging in, or submitting data. jQuery gives you a
clean and simple way to:
🔹 2. Getting and Setting
Input Values
✅ Accessing Values
let
name = $("#username").val(); // Get
$("#username").val("John"); // Set
|
Selector |
Method |
Description |
|
input/textarea |
.val() |
Get or set
value |
|
checkbox |
.prop('checked') |
Check if
selected |
|
select |
.val() |
Get selected
value |
✅ Example:
<input
type="text" id="email" />
<button
id="getEmail">Check</button>
$("#getEmail").click(function(){
alert("Email: " +
$("#email").val());
});
🔹 3. Real-Time Input
Validation
✅ Keyup Event + Basic Check
$("#username").on("keyup",
function() {
let value = $(this).val();
if(value.length < 3){
$("#error").text("Username
too short");
} else {
$("#error").text("");
}
});
Show instant feedback as the user types = better UX.
🔹 4. Form Validation on
Submit
$("form").on("submit",
function(e){
e.preventDefault(); // Prevent default reload
let email = $("#email").val();
if(email === ""){
alert("Email is required.");
return;
}
// Proceed with submission logic
});
✅ Combine this with .test() and
regex for deeper validation.
🔹 5. Working with
Checkboxes & Radios
if($("#agree").prop("checked"))
{
console.log("User agreed to
terms.");
}
|
Action |
jQuery
Code |
|
Check status |
.prop("checked") |
|
Set value |
.prop("checked",
true) |
|
Toggle |
$('#cb').prop('checked',
!this.checked) |
🔹 6. Select Dropdown
Handling
let plan = $("#planSelect").val(); //
Get selected
$("#planSelect").val("pro"); // Set selected
✅ Example:
<select
id="planSelect">
<option
value="free">Free</option>
<option
value="pro">Pro</option>
</select>
$("#planSelect").change(function(){
alert("Plan selected: " +
$(this).val());
});
🔹 7. Preventing Default
Behavior
Use this to stop forms from refreshing the page:
$("form").submit(function(e){
e.preventDefault();
// your logic here
});
✅ Essential when handling forms
via JavaScript or AJAX.
🔹 8. Submit Form with
jQuery AJAX
<form
id="loginForm">
<input type="text"
id="user">
<input type="password"
id="pass">
<button
type="submit">Login</button>
</form>
$("#loginForm").on("submit",
function(e){
e.preventDefault();
let data = {
user: $("#user").val(),
pass: $("#pass").val()
};
$.post("/api/login", data,
function(response){
alert("Login Success!");
}).fail(function(){
alert("Login failed.");
});
});
🔹 9. Showing/Hiding
Submit Button Based on Validation
$("#email").on("keyup",
function(){
let isValid =
$(this).val().includes("@");
$("#submit").prop("disabled", !isValid);
});
✅ Encourages users to correct
mistakes before submitting.
🔹 10. Summary Table:
jQuery Form Controls
|
Task |
jQuery
Code |
|
Get input
value |
$("#field").val() |
|
Set input
value |
$("#field").val("value") |
|
Check if
checkbox is checked |
$("#cb").prop("checked") |
|
Get dropdown
selected |
$("#select").val() |
|
Prevent form
from reloading |
e.preventDefault()
in .submit() |
|
AJAX form
submission |
$.post("/url",
data) |
jQuery is a fast, small JavaScript library that simplifies HTML document traversal, manipulation, event handling, and AJAX.
Yes — while modern frameworks exist, jQuery remains heavily used in WordPress, older projects, CMSs, and quick prototypes.
It shortens code, handles cross-browser issues, simplifies AJAX, and is beginner-friendly.
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load.
$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load
✅ Yes — it’s open-source under the MIT license.
jQuery is a library built with JavaScript to simplify tasks like DOM handling and AJAX.
✅ Yes — it works across all major desktop and mobile browsers.
It’s better to learn JavaScript fundamentals first, but jQuery is easier to grasp early on for quick UI work.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)