This is driving me crazy. I have tried every different ways to do this. Trying to show/hive a div based on a radio box selection. Below is my code into my main.js :
$("input[name='payment_method']").click(function () {
if ($(this).attr("id") == "payment_method_cod") {
$("#checkout_insurance").show();
} else {
$("#checkout_insurance").hide();
}
});
HTML:
<div id="checkout_insurance">
<h2>Checkout with Insurance</h2>
</div>
<div id="payment" class="woocommerce-checkout-payment">
<ul class="payment_methods methods">
<li class="payment_method_cod">
<input id="payment_method_cod" type="radio" class="input-radio" name="payment_method" value="cod" data-order_button_text="" checked="checked">
<label for="payment_method_cod">
...
</li>
<li class="payment_method_firstdata">
<input id="payment_method_firstdata" type="radio" class="input-radio" name="payment_method" value="firstdata" data-order_button_text="">
<label for="payment_method_firstdata">
....
</li>
</ul>
</div>
I cant get to work. If I place another cdn of the jQuery on the footer, the function works, however messes up a lot of other stuff. jQuery is being loaded on the header. I am current using jQuery 1.11.3.
Any help will be appreciated.
Thank you!
Use
:checked
selector to check if the radio element with idpayment_method_cod
is checked or not.$('#payment_method_cod:checked')
will returntrue/false
based on the condition if the radio button is checked or not andtoggle
it, whentrue
, the element#checkout_insurance
will be shown.Assuming the id’s used in the code are unique.
Demo
Please use
.change()
on checkbox use.click()
on button.Try this approach this will select change event on checkbox and check if the id of the checkbox is
payment_method_cod
it will then show or hide depending on checked propertydemo
Changing your code a bit :
});