I have a Gravity Form where I dynamically populate a drop-down and a second field with the count of the items in that dropdown.
If the count of items is >1, I use the selected item info to go get some additional info and use it to populate a second drop-down. This works fine. Here's the relevant section of code:
// When a choice from the dropdown "Which League (F32)" is selected,
// populate "League ID (F57)",
// populate "League Name (F64)" and
// populate dropdown "Player Two (F13)"
jQuery("#input_5_32").on('change',function(){
var myval = jQuery(this).val();
jQuery("#input_5_57").val(myval);
jQuery("#input_5_64").val(jQuery('#input_5_32 option:selected').text());
jQuery.ajax({
type: 'post',
dataType:'JSON',
url : bb_ajax_url,
data : { league_id: myval, action:'get_player_two_names' },
success : function( data ) {
jQuery('#input_5_13').html('');
jQuery('#input_5_13').append(jQuery('<option value="">Select Player Two</option>'));
jQuery.each(data , function(k,p){
jQuery('#input_5_13').append(jQuery('<option value="'+p.v+'">'+p.t+'</option>'));
});
},
});
});
Most of the time, however, there's only one item in the 1st drop-down, in which case, I don't need the user to open it and click on the item to make a selection, I already know what it's going to be. I'd like to use the info and trigger the same routine which dynamically populates the 2nd drop-down so I can hide the 1st drop-down and save the user a little time and irritation.
The working module gets triggered based on a change to the 1st drop-down. Can I trigger this behavior some other way, or perhaps fake the selection from drop-down #1?
Got my plugin conflict out of the way so I tried again to implement @Wouter Bouwman's answer, though I'm probably making a mistake somewhere. Here's what I did:
//Check whether the amount of options is only one
if (jQuery('#input_5_32').length === 1) {
jQuery('#input_5_32').change(); //Trigger the change event
console.log("Yes, Routine to not need 'Which League' has been executed");
var myval = jQuery(this).val();
console.log(myval);
jQuery("#input_5_57").val(myval);
jQuery("#input_5_64").val(jQuery('#input_5_32 option:selected').text());
jQuery.ajax({
type: 'post',
dataType:'JSON',
url : bb_ajax_url,
data : { league_id: myval, action:'get_player_two_names' },
success : function( data ) {
jQuery('#input_5_13').html('');
jQuery('#input_5_13').append(jQuery('<option value="">Select Player Two</option>'));
jQuery.each(data , function(k,p){
jQuery('#input_5_13').append(jQuery('<option value="'+p.v+'">'+p.t+'</option>'));
});
}
});
}
the #input_5_32 isn't getting filled with anything, and the #input_5_64 is getting filled with "Select the League for this Match" instead of the league Name. The dropdown #input_5_13 still says "Select Player Two".
I must be implementing the suggestion incorrectly but need a little assistance here.