Last active
January 20, 2024 04:17
-
-
Save Qubadi/16201068d168313d43a8abbc3fff40ff to your computer and use it in GitHub Desktop.
Jetsmartfilters: This function is the core of the script. It checks whether all required fields are filled out
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
1. Add your page slug name, where you have the search form. | |
2. In summary, the script dynamically enables or disables the "Apply" button based on whether all | |
specified fields on the page are filled out, ensuring that the button is only clickable when all required fields have data. | |
function custom_required_field_script() { | |
// Only add this script on the specific page with the slug 'YOUR PAGE SLUG NAME/' | |
if (strpos($_SERVER['REQUEST_URI'], 'YOUR PAGE SLUG NAME/') !== false) { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
// Function to check the state of the fields and toggle the button's disabled state | |
function toggleApplyButton() { | |
var allFieldsFilled = true; | |
// Check if select filter is filled | |
if ($('.jet-smart-filters-select .jet-select__control').val() === '') { | |
allFieldsFilled = false; | |
} | |
// Check if search filters are filled | |
$('.jet-smart-filters-search .jet-search-filter__input').each(function() { | |
if ($(this).val() === '') { | |
allFieldsFilled = false; | |
} | |
}); | |
// Enable or disable the apply button based on the fields' state | |
$('.jet-smart-filters-apply-button .apply-filters__button').prop('disabled', !allFieldsFilled); | |
} | |
// Initial check on page load | |
toggleApplyButton(); | |
// Check the state of the fields whenever they change | |
$('.jet-smart-filters-select .jet-select__control, .jet-smart-filters-search .jet-search-filter__input').on('change keyup', function() { | |
toggleApplyButton(); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action('wp_footer', 'custom_required_field_script'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment