Created
July 9, 2020 20:43
-
-
Save wpmudev-sls/b50025b9b2b8faeb75897bbe4395f966 to your computer and use it in GitHub Desktop.
[Forminator] - Toggle field visibility. The following snippet will allow to toggle a field's visibility based on another field's value
This file contains 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
<?php | |
/** | |
* Plugin Name: [Forminator] - Toggle field visibility | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: The following snippet will allow to toggle a field's visibility based on another field's value | |
* Task: SLS-316 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'wp_footer', function(){ | |
?> | |
<script type="text/javascript"> | |
(($,d)=>{ | |
if ( window.wpmudev_forminator_toggle_calculation_field ) { | |
return; | |
} | |
window.wpmudev_forminator_toggle_calculation_field = { | |
target_field : '', | |
source_field : '', | |
run: function(){ | |
let field_1_id = 'calculation-1', | |
field_2_id = 'calculation-2'; | |
this.target_field = $( `.forminator-custom-form #${field_2_id}-field` ); | |
this.source_field = $( `.forminator-custom-form #${field_1_id}-field` ); | |
this.target_field.on( 'change', wpmudev_forminator_toggle_calculation_field.toggle_visibility ); | |
//setTimeout(function(){ wpmudev_forminator_toggle_calculation_field.hide_field( wpmudev_forminator_toggle_calculation_field.target_field ); }, 100); | |
setTimeout(function(){ wpmudev_forminator_toggle_calculation_field.target_field.trigger( 'change' ); }, 100); | |
}, | |
toggle_visibility: function(){ | |
let self = wpmudev_forminator_toggle_calculation_field, | |
el = $( this ), | |
target_value = el.val(), | |
source_value = self.source_field.val(); | |
// Hide calculation-2 field when calculation-2 value is greater than calculation-1 value | |
if ( target_value > source_value ) { | |
self.hide_field( el ); | |
} else { | |
self.show_field( el ); | |
} | |
}, | |
hide_field: function ( field ) { | |
let row = field.closest( '.forminator-row' ), | |
col = row.find( '.forminator-col' ); | |
row.addClass( 'forminator-hidden' ); | |
col.addClass( 'forminator-hidden' ); | |
}, | |
show_field: function ( field ) { | |
let row = field.closest( '.forminator-row' ), | |
col = row.find( '.forminator-col' ); | |
row.removeClass( 'forminator-hidden' ); | |
col.removeClass( 'forminator-hidden' ); | |
} | |
} | |
$( d ).ready( function(){ | |
wpmudev_forminator_toggle_calculation_field.run(); | |
} ); | |
})(jQuery,document); | |
</script> | |
<?php | |
}, 999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment