I am using Select2 within WooCommerce in some of my own custom areas and I am targeting it with some code to add and removes certain classes and it's working fine except for the SelectWoo instances used by WooCommerce themselves do not add the class using el.addClass('has-selection');
in the example provided.
Example code:
(function($) {
$('select').each(function(e) {
handleSelectSelections($(this));
});
})( jQuery );
function handleSelectSelections(select) {
var el = (select.next('.select2').length) ? jQuery(select.data('select2').$container) : select;
if (select.val() !== "" && select.val() !== null) {
el.addClass('has-selection');
} else {
el.removeClass('has-selection');
}
}
Everything works fine, except when it gets to the actual part where it adds the class it doesn't work - no class is added.
Am I missing something here?
handleSelectSelections
trigger on select change, or only once? Cannot help without working example, do you have an online page? – Elisaelisabet