javascript - add and remove class when toggling through radio button -


i trying add , remove class based on radio button behavior. idea if selected radio button value paypal class="required" form input fields gets removed , when toggled credit card fields got class required removed them gets required class back. here how doing it

$('input[name*="payment"]').on('change', function() {   var type = this.value;   switch (type) {     case 'paypal':       $('#payment input').each(function() {         if ($(this).hasclass('required')) {           $(this).removeclass('required');           $(this).addclass('requiredfalse');         }       });     case 'creditcard':       $('#payment input').each(function() {         if ($(this).hasclass('requiredfalse')) {           $(this).addclass('required');           $(this).removeclass('requiredfalse');         }       });   } }); 

here fiddle. cant figure out doing wrong.

https://jsfiddle.net/sghoush1/d225cdrp/1/

you switch-case missing breaks. when type "paypal" both cases executed, how switch works. try this:

$('input[name*="payment"]').on('change', function() {   var type = this.value;   switch (type) {     case 'paypal':       $('#payment input').each(function() {         if ($(this).hasclass('required')) {           $(this).removeclass('required');           $(this).addclass('requiredfalse');         }       });       break;     case 'creditcard':       $('#payment input').each(function() {         if ($(this).hasclass('requiredfalse')) {           $(this).addclass('required');           $(this).removeclass('requiredfalse');         }       });       break;   } }); 

Comments