0

Try show/hide a field when checked a checkbox. How can i do this with jQuery?

Thats my code:

<input type="checkbox" class="input-checkbox " name="billing_wcj_checkout_field_2" id="billing_wcj_checkout_field_2" value="1">

<input type="text" class="input-text " name="billing_eu_vat_number" id="billing_eu_vat_number" placeholder="EU VAT Number" value="">

<script>
    jQuery(function() {
            jQuery('input[type="checkbox"]').on('change', function() {
                jQuery(this).find('#billing_eu_vat_number_field').toggle(!this.checked);
            });
        });
    });
</script>

Screenshot

1
  • For one thing, the id you are searching for in the jqeury is incorrect, you have add _field to it. Commented Mar 21, 2018 at 11:55

2 Answers 2

1

You can do it like that:

    jQuery(function() {
            jQuery('.input-checkbox').change(function() {
                jQuery('#billing_eu_vat_number').toggle();
            });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="input-checkbox " name="billing_wcj_checkout_field_2" id="billing_wcj_checkout_field_2" value="1">

<input type="text" class="input-text " name="billing_eu_vat_number" id="billing_eu_vat_number" placeholder="EU VAT Number" value="">

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. At this moment i used the code from Smit Raval , but i'm happy also with your answer. So i can learn a lot how coding 'best practice' in jQuery :-)
0

HTML

<input type="checkbox" class="input-checkbox " name="billing_wcj_checkout_field_2" id="billing_wcj_checkout_field_2" value="1">

<input type="text" class="input-text " name="billing_eu_vat_number" id="billing_eu_vat_number" placeholder="EU VAT Number" style="display:none;" value="">

JS

$(document).ready(function(){
  $("#billing_wcj_checkout_field_2").click(function(){
    if($(this).is(":checked")){
      $("#billing_eu_vat_number").show();
    }else{
      $("#billing_eu_vat_number").hide();
    }
  });

});

Working pen link

enter link description here

3 Comments

Please approve the answer if it helped. :) Thanks @Tuxy
Ok, i'm sorry! forget :-/
No issue. Thanks @Tuxy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.