I would like to create a jQuery event when a particular option is selected. I am familiar how to do this with a general dropdown structure, but mine is a but unusual and it doesn't work:
HTML
<span class="input-wrapper">
<select name="country" id="billing_country" class="country_select" autocomplete="country" tabindex="-1" aria-hidden="true">
<option value="" selected="selected">Select a country…</option>
<option value="BE">Belgium</option>
<option value="NL" selected="selected">Netherlands</option>
</select>
<span class="select2-container" dir="ltr" style="width: 100%;">
<span class="selection">
<span class="select2-selection" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-billing_country-container" role="combobox">
<span class="select2-selection__rendered" id="select2-billing_country-container" role="textbox" aria-readonly="true" title="Belgium" style="color: rgb(51, 51, 51);">Belgium</span>
</span>
</span>
</span>
</span>
My jQuery attempt:
jQuery('#billing_country').change(function(){
jQuery(document).ajaxComplete( function() {
if( jQuery('#select2-billing_country-container').attr('title') == 'Netherlands'){
jQuery('#billing_address_2_field').addClass('hide'); // random event
}
});
});
The above situation is that the <option> is not changing with the selected="selected" attribute when you select another country. The only thing that changes is the attribute title from #select2-billing_country-container. And when you change the selection Ajax is also involved.
What am I missing or how can this be better?
Update
The above jQuery was in general correct, it had some conflicts with other jQuery functions. The only thing that it made it different, was the fact that on page load the dropdown structure is using the general <select><option> elements, but when selecting a different option it turns into an Ajax request, using the <span>. So above and with Doug's answer gave a full result.
checkout-page.js