Advertisement
sierre

Sail Number and Country Code - Custom Codes

Dec 13th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2. // This is added to:
  3. // /wp-admin/admin.php?page=edit-snippet&id=13
  4.  
  5. add_action('wp_footer', 'sails_number_and_country_code_func', 999999);
  6. function sails_number_and_country_code_func(){
  7. ?>
  8. <style>
  9.     .enter_your_sails_number_and_country_code_here { display: block; }
  10. </style>
  11. <script>
  12. jQuery(document).ready(function($) {
  13.     // Ensure the DOM is fully loaded
  14.     const targetRow = $('.enter_your_sails_number_and_country_code_here');
  15.     const textarea = targetRow.find('textarea'); // Locate the textarea inside the target row
  16.  
  17.     if (targetRow.length) {
  18.         targetRow.css('display', 'block');
  19.     }
  20.  
  21.     // Use event delegation to handle dynamically added elements
  22.     $(document).on('change', '#pa_sail-numbers', function() {
  23.         console.log('Selected value:', $(this).val());
  24.         if ($(this).val() == "yes-i-need-sail-numbers") {
  25.             targetRow.css('display', 'table-row');
  26.             // Add "required" attribute to the textarea
  27.             textarea.attr('required', true);
  28.         } else {
  29.             targetRow.css('display', 'none');
  30.             // Remove "required" attribute from the textarea
  31.             textarea.removeAttr('required');
  32.         }
  33.     });
  34.  
  35.     // Handle dynamic changes after an AJAX call
  36.     jQuery(document).ajaxComplete(function(event, xhr, settings) {
  37.         targetRow.css('display', 'block');
  38.         // Optionally add "required" attribute after AJAX completion if necessary
  39.         textarea.attr('required', true);
  40.     });
  41. });
  42. </script>
  43.  
  44.  
  45. <?php
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement