Hey all I am trying to run a function and display a message when a user selects any other country than the US. However, I’m stuck as I can’t even get the select box to log the event on changing of the country. Here’s my code:
function checkIfInternational(e) {
console.log(e);
}
const shipCountry = document.querySelector("#billing_country"); //Logs the select box
shipCountry.addEventListener("change", checkIfInternational); // listening for the change event
Any ideas on why this is happening? Is Woocommerce preventing JS from running on the page or something?
Edit: JS Fiddle: https://jsfiddle.net/suoc57mg/1/
2
Answers
Assuming that your #billing_country element looks like this:
You can check its value by target’s value of event which you handle in this way.
You need to delegate your event to the document body, otherwise WooCommerce will block your code on checkout.
As WooCommerce JS code uses already jQuery, try the following:
It should work now.