Search code examples
javascriptdomchaining

DOM event chaining not working


So, any hints why this is not working? I'm trying to get both input fields ('pagamento' and 'parcelas') at a preset if the 'Boleto' radiobox is selected, ('1' and 'MercadoPago', respectively, and back again to user input if the 'Cartão' radiobox is selected.

function boleto() {
    if(document.getElementById('tipodepagamento').checked==true){
        document.getElementById('pagamento').value = 'MercadoPago';
        document.getElementById('pagamento').disabled = true;
        document.getElementById('parcelas').value = '1';
        document.getElementById('parcelas').disabled = true;
    }
    else{
        document.getElementById('pagamento').disabled = false;
        document.getElementById('parcelas').disabled = false;
    } 
}
<div class="col-md-6">
    <label class="radio-inline">
        <input type="radio" name="tipodeconta" id="tipodepagamento2" value="Cartão" checked onClick="boleto();">Cartão de Crédito
    </label>
</div>
<div class="col-md-6">
    <label class="radio-inline">
        <input type="radio" name="tipodeconta" id="tipodepagamento" value="Boleto" onClick="boleto();">Boleto
    </label>
</div>
<label>Plataforma de Pagamento</label>
<select class="form-control" onClick="fpagamento();validador();" onKeyUp="fpagamento();" name="pagamento" id="pagamento">
    <option value="Selecione">Selecione</option>
    <option value="Paypal">Paypal</option>
    <option value="MercadoPago">Mercado Pago</option>
</select>
<label>Número de Parcelas</label>
<select class="form-control" id="parcelas" name="parcelas">
    <option value="1">1x</option>
    <option value="2">2x</option>
</select>


Solution

  • Fiddle = http://kodeweave.sourceforge.net/editor/#d692f9461c022ee386cb2c5329f453c4

    I see a few problems with your code.

    <input type="radio" name="tipodeconta" id="tipodepagamento" value="Boleto" onClick="boleto();">
    <select class="form-control" onClick="fpagamento();validador();" onKeyUp="fpagamento();"
    

    First off you shouldn't use onClick for a radio button, or a select element. use onChange instead which means you won't need to call onKeyUp="fpagamento();"

    BTW: your code is very WET. In addition it's bad practice to add a onClick attribute in your html (same goes for other events). Put your javascript in a .js file and edit it there.

    var tipodeconta = $('input[name=tipodeconta]'),
        cartao      = document.querySelector('#tipodepagamento2'),
        pagamento   = $('#pagamento'),
        checkThis   = $('.form-control');
    
    tipodeconta.on('change', function() {
      if (cartao.checked) {
        pagamento.val('MercadoPago');
      }
      (cartao.checked) ? checkThis.prop('disabled', true) : checkThis.removeAttr('disabled');
    }).trigger('change');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="panel panel-default">
      <div class="panel-body">
        <div class="col-md-6">
          <label class="radio-inline">
            <input type="radio" name="tipodeconta" id="tipodepagamento2" value="Cartão" checked=""> 
            Cartão de Crédito
          </label>
        </div>
        <div class="col-md-6">
          <label class="radio-inline">
            <input type="radio" name="tipodeconta" id="tipodepagamento" value="Boleto">
            Boleto
          </label>
        </div>
      </div>
    </div>
    
    <label>Plataforma de Pagamento</label>
    <select class="form-control" name="pagamento" id="pagamento" disabled="">
      <option value="Selecione">Selecione</option>
      <option value="Paypal">Paypal</option>
      <option value="MercadoPago">Mercado Pago</option>
    </select>
    <label>Número de Parcelas</label>
    <select class="form-control" id="parcelas" name="parcelas" disabled="">
      <option value="1">1x</option>
      <option value="2">2x</option>
    </select>