Advertisement
CodeDropz

Combine Fields Into Hidden - CF7

Oct 26th, 2023
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. add_action('wp_head', function(){
  2.     ?>
  3.         <script type="text/javascript">
  4.             document.addEventListener('DOMContentLoaded', function () {
  5.  
  6.               // Define an array of field Name
  7.               var fieldNames = ['your-name', 'your-lastname'];
  8.  
  9.               var hiddenField = document.querySelector('input[name="upload-folder"]');
  10.  
  11.               fieldNames.forEach(function (fieldName) {
  12.                 var field = document.querySelector('input[name="' + fieldName + '"]');
  13.                 if( field ){
  14.                     field.addEventListener('input', updateHiddenField);
  15.                 }
  16.               });
  17.  
  18.               function updateHiddenField() {
  19.                 if (hiddenField) {
  20.                   var combinedValue = fieldNames.map(function (fieldName) {
  21.                     var field = document.querySelector('input[name="' + fieldName + '"]');
  22.                     return field ? field.value : '';
  23.                   }).join('-');
  24.                   hiddenField.value = combinedValue;
  25.                 }
  26.               }
  27.  
  28.             });
  29.         </script>
  30.     <?php
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement