I have this <select>
that when a user selects an option in there, performs an AJAX to the backend then show/hide additional <select>
:
$('#select1').on('change', function() {
// Get fruit information
$.ajax({
type: 'POST',
url: '/path/to/ajax',
data: {id: $(this).val()},
dataType: 'json',
success: function (data) {
if (data.type == "apple") {
$('#select2').addClass('hide');
$('#select3').addClass('hide');
}
else if (data.type == "banana") {
$('#select2').removeClass('hide');
$('#select3').removeClass('hide');
}
}
});
});
This works fine. When I submit the form the backend performs a validation then if it fails it loads the page again, however, the #select1
has the value I chosen earlier but the other <select>
that suppossed to show is hidden. Is it possible to trigger $('#select1').on('change', function()
when the page loads?