Search code examples
javascriptjqueryradio-buttonstripe-payments

Need to change radio value if a input has a specific class


I'm desperate.
I'm making a credit card auto system using Stripe Payment plugin, but I'm kind of lost. Anyway, when the credit card is MasterCard (for example), the card number input receive "mastercard" class - what give it a background image with the flag of mastercard.
But I need to chance the value of a radio input with it. So here is my unfunctional code:

HTML:

<input id="numero" type="tel" class="validate" required  name="cartaoNumero" disabled='disabled' style="transition:none;" />
<input type="radio" id="credito1" name="codigoBandeira" value="visa" />

JQUERY (or attempt to):

    function mudarradio() {
        if ($("#numero").hasClass( "visa" )) {

            $('input:radio[name=codigoBandeira]').val(['visa']);

            } else if ($("#numero").hasClass( "mastercard" )) {

            $('input:radio[name=codigoBandeira]').val(['mastercard']);
        }
        $('body').on('mouseenter', mudarradio);
    };

Like, when the user use a Mastercard number, #numero input receive mastercard class.
Nothing happens even when #numero has a different class...
Ok, I know, it is a mess. But I'm a front-end progammer, so.. HELPP!!!!


Solution

  • I would start with changing this line $('body').on('mouseenter', mudarradio);

    to target a change to the input box, rather than a mouse entering the body? Is there a reason you set it up that way? try this instead

    $('#numero').on('change', mudarradio);