Think you’d have to move the “selected” attribute to the correct option, and then call the “Tell Chosen the select box has changed” function as specified in their documentation…
// tell Chosen that a select has changed
$('.my_select_box').trigger('chosen:updated');
(function() {
'use strict';
function setDefaultValue() {
var selectElement = document.querySelector('select[name="formatoOut"]');
if (selectElement) {
var options = selectElement.options;
for (var i = 0; i < options.length; i++) {
if (options[i].value === "XLS") {
options[i].selected = true;
break;
}
}
// Trigger the "change" event to update the Chosen combobox
var event = new Event('change', { bubbles: true });
selectElement.dispatchEvent(event);
}
}
// Wait for the page to load
window.addEventListener('load', function() {
// Call the function to set the default value
setDefaultValue();
});
})();
(function() {
'use strict';
// Wait for the document to be fully loaded
document.addEventListener('DOMContentLoaded', function() {
// Function to check if Chosen is loaded
function isChosenLoaded() {
return typeof HTMLElement.prototype.chosen !== 'undefined';
}
// Function to set the default value
function setDefaultValue() {
var selectElement = document.querySelector('select[name="formatoOut"]');
if (selectElement) {
selectElement.value = 'XLS';
if (isChosenLoaded() && typeof Event === 'function') {
var event = new Event('chosen:updated', { bubbles: true, cancelable: true });
selectElement.dispatchEvent(event);
}
}
}
// Call the function to set the default value
setDefaultValue();
});
})();