jQuery( document ).ready(function( $ ) { $( ".intdir-select-field" ).each( function() { intdirFormHandler( this ); }); $( ".intdir-select-field" ).change( function() { intdirFormHandler( this ); }); }); function intdirFormHandler( field ) { ( function( $ ) { var value = $( field ).val(); var data_field = $( field ).parents( '.intdir-widget-main' ).find( '.intdir-data-field' ); if( value < 1 ) { $( data_field ).hide(); } else { $( data_field ).show(); var req = 'https://grad.wisc.edu/wp-content/plugins/intdir-widget/get_int_dir.php?val=' + value; $.getJSON( req, function( data ) { $.each( data, function( key, val ) { $( data_field ).find( "." + key + " > .data" ).html( val ); }); }); } })( jQuery ); }