I am working with WooCommerce on WordPress. In the checkout I want to check if a certain radio button is selected before the user submits. If a certain id is selected, then run a window.alert
for now. I have the following form being served up by WooCommerce:
<ul id="shipping_method">
<li>
<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_legacy_local_pickup" value="legacy_local_pickup" class="shipping_method">
<label for="shipping_method_0_legacy_local_pickup">Local Pickup</label>
</li>
<li>
<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_distance_rate_shipping" value="distance_rate_shipping" class="shipping_method" checked="checked">
<label for="shipping_method_0_distance_rate_shipping">Delivery from 536 S College Ave, Chicago, IL</label>
</li>
</ul>
I am using the following script so far to test:
<script type='text/javascript'>
if (document.getElementById('shipping_method_0_distance_rate_shipping').checked) {
alert("Delivery checked!");
}
</script>
The script works great when page loads, but if I deselect and select the radio button again before submitting page the script does not function. If I can see which they are selecting, in real time, that would solve my immediate issue.
I appreciate your time and help with this.