π‘ jQuery Snippet: Enable Button on “Yes” Confirmation
When designing forms that require user confirmation (like agreeing to terms or confirming intent), it’s a common practice to disable the Submit or Insert button until a certain condition is met — such as the user selecting "Yes".
Here’s a handy jQuery example and a breakdown of how it works:
✅ What This Script Does
This jQuery snippet ensures that a button (with ID InsertButton
) remains disabled until the user selects a specific radio button (#pawb_confirm_1
, which represents "Yes"):
π Line-by-Line Breakdown
Ensures the DOM is fully loaded before executing any script.
Disables the Insert button by default on page load.
Adds a change
event listener to both radio buttons (assumed to be "No" and "Yes" options).
Checks if the "Yes" radio button is selected.
If yes → enables the button.
If not → keeps it disabled.
π§ Why .prop()
?
The jQuery .prop()
method is used here to get and set DOM element properties (like checked
and disabled
), which are dynamic and change based on user interaction.
Common .prop()
Examples:
jQuery Code | Description |
---|---|
$('#agree').prop('checked') | Gets if a checkbox is checked |
$('#button').prop('disabled', true) | Disables a button |
$('#option').prop('selected', true) | Selects a dropdown option |
Use .prop()
when working with:
-
checked
-
disabled
-
selected
-
readonly
π Compared to .attr()
, which is used for:
-
href
-
src
-
title
-
data-*
attributes
π― Real-World Use Case
Imagine a form where users must confirm an action before proceeding:
With the script above:
-
The Insert button is disabled at first.
-
Only selecting "Yes" enables the button.
Comments
Post a Comment