|
| 1 | +/** |
| 2 | + * Gravity Wiz // Gravity Forms // Swap Values in Drop Down & Multiselect Fields |
| 3 | + * https://gravitywiz.com/ |
| 4 | + * |
| 5 | + * This snippet will add a swap button for any two Drop Down or Multiselect fields. When clicked the value(s) |
| 6 | + * from each field will be swapped with the value(s) in the other. Both fields must be of the same type and have |
| 7 | + * the same choices available. You may need to tweak the styling/position of the swap button depending on your theme. |
| 8 | + * |
| 9 | + * Instructions: |
| 10 | + * |
| 11 | + * 1. Install this snippet with our free Custom JavaScript plugin. |
| 12 | + * https://gravitywiz.com/gravity-forms-custom-javascript/ |
| 13 | + * |
| 14 | + * 2. Following the inline instructions to configure for your form. |
| 15 | + */ |
| 16 | +// Replace "4" and "5" with field IDs for any two Drop Down or Multiselect fields. Field types must match. DUplicate |
| 17 | +gwCreateSwapButton( GFFORMID, 4, 5 ); |
| 18 | + |
| 19 | +function gwCreateSwapButton( formId, fieldIdA, fieldIdB ) { |
| 20 | + |
| 21 | + const swapButton = document.createElement('button'); |
| 22 | + swapButton.innerText = '⇄'; |
| 23 | + swapButton.type = 'button'; |
| 24 | + swapButton.style.position = 'absolute'; |
| 25 | + swapButton.style.padding = '0'; |
| 26 | + swapButton.style.background = 'transparent'; |
| 27 | + swapButton.style.color = '#000'; |
| 28 | + swapButton.style.boxShadow = 'none'; |
| 29 | + swapButton.style.top = '0'; |
| 30 | + swapButton.style.right = '-15px'; |
| 31 | + swapButton.style.border = '0'; |
| 32 | + |
| 33 | + // Insert the button between the two dropdowns |
| 34 | + const dropdownA = document.getElementById(`input_${formId}_${fieldIdA}`); |
| 35 | + const dropdownB = document.getElementById(`input_${formId}_${fieldIdB}`); |
| 36 | + |
| 37 | + dropdownA.parentNode.style.position = 'relative'; |
| 38 | + dropdownA.parentNode.appendChild(swapButton); |
| 39 | + |
| 40 | + // Add event listener to handle the swap functionality |
| 41 | + swapButton.addEventListener('click', function() { |
| 42 | + |
| 43 | + let selectedA; |
| 44 | + let selectedB; |
| 45 | + |
| 46 | + if ( dropdownA.multiple ) { |
| 47 | + |
| 48 | + selectedA = Array.from(dropdownA.selectedOptions).map(option => option.value); |
| 49 | + selectedB = Array.from(dropdownB.selectedOptions).map(option => option.value); |
| 50 | + |
| 51 | + Array.from(dropdownA.options).forEach(option => { |
| 52 | + option.selected = selectedB.includes(option.value); |
| 53 | + }); |
| 54 | + |
| 55 | + Array.from(dropdownB.options).forEach(option => { |
| 56 | + option.selected = selectedA.includes(option.value); |
| 57 | + }); |
| 58 | + |
| 59 | + } else { |
| 60 | + |
| 61 | + selectedA = dropdownA.value; |
| 62 | + selectedB = dropdownB.value; |
| 63 | + |
| 64 | + dropdownA.value = selectedB; |
| 65 | + dropdownB.value = selectedA; |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + dropdownA.dispatchEvent(new Event('change')); |
| 70 | + dropdownB.dispatchEvent(new Event('change')); |
| 71 | + |
| 72 | + }); |
| 73 | +} |
0 commit comments