(function ($, Drupal, once) { Drupal.behaviors.structuralPagesSelect2 = { attach: function (context, settings) { function initSelect2() { var elements = once('structural-pages-select2', 'select.select2-widget', context); if (elements.length) { var $select = $(elements); // If the select2 jQuery plugin is loaded (by another module/theme), use it. // Otherwise, try chosen if it's there. if (typeof $.fn.select2 !== 'undefined') { $select.select2({ width: '100%', dropdownAutoWidth: true }); } else if (typeof $.fn.chosen !== 'undefined') { $select.chosen({ width: '100%', search_contains: true }); } } } // Initialize on load initSelect2(); // Some admin themes replace select elements entirely. This ensures we hook in. $(document).on('ajaxComplete', function () { initSelect2(); }); } }; })(jQuery, Drupal, once);