Last active
April 7, 2026 00:47
-
-
Save cliffordp/c0b48b14f0f6f42d27c79e8f39013774 to your computer and use it in GitHub Desktop.
Gravity Forms: add the READONLY property to an input UNTIL it gets a value set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * This snippet: https://gist.github.com/cliffordp/c0b48b14f0f6f42d27c79e8f39013774 | |
| * WHAT: This adds the READONLY property to an input field and then removes it AFTER this same field has a value in it. | |
| * WHY: Useful when a text field gets a dynamically-filled value but you want the input to remain editable thereafter. | |
| * EXAMPLE: a custom County text field that gets filled upon selecting a different field's Address Autocomplete. | |
| * HOW-TO: | |
| * 1. Paste this snippet into https://gravitywiz.com/gravity-forms-code-chest/ (this code is formatted for use in that plugin). | |
| * 2. Add the `readonly-until-value` class to any text input field (not tested on other field types). | |
| * 3. Alternatively, add a class like `readonly-until-value-17` to make Field#17 (the Address Autocomplete field) focused (outlined) upon clicking the readonly field. | |
| */ | |
| gform.addAction("gfcc_deferred", function () { | |
| jQuery("#gform_GFFORMID .gfield").each(function () { | |
| var classes = jQuery(this).attr("class") || ""; | |
| var match = classes.match(/readonly-until-value(?:-(\d+))?(?:\s|$)/); | |
| if (!match) return; | |
| var sourceFieldId = match[1] || null; | |
| var $input = jQuery(this).find("input"); | |
| var $field = jQuery(this); | |
| $input | |
| .prop("readonly", true) | |
| .attr("title", "first, enter the Full Address"); | |
| // Hidden until value is set — no layout shift | |
| $field.css("visibility", "hidden"); | |
| if (sourceFieldId) { | |
| $input.on("focus", function () { | |
| jQuery("#input_GFFORMID_" + sourceFieldId).focus(); | |
| }); | |
| } | |
| $input.on("change", function () { | |
| if ($input.val()) { | |
| $field.css("visibility", ""); | |
| $input | |
| .prop("readonly", false) | |
| .removeAttr("title") | |
| .off("change focus"); | |
| } | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment