Function in jquery button disabled

2

How do I make a div appear when a button is disabled, and when enabled I want this div to be hidden

 $(document).on('ready', '#btn-cadastra-atividade', function () 
    { 
    $this = $(this)
    if($this.attr('disabled') === true)
    { 
       $('#erroAtividade').show(); 
    }
    else
    {   
    $('#erroAtividade').hide(); 
    }});
    
asked by anonymous 01.08.2017 / 16:16

2 answers

0

I've done an example where it shows the two button statuses:

function divOnButton() {
  if ($('#btn1').prop('disabled')) {
    $('#div1').show();
  } else {
    $('#div1').hide();
  }
}

function statusOnButton() {
  $('#btn1').prop('disabled', !$('#btn1').prop('disabled'));
  divOnButton()
}
$(document).ready(function() {
  divOnButton();
  $("#btnStatus").click(function() {
    statusOnButton();
  });
});
#div1 {
  border: 1px solid #000;
}
#conteudo {
  height:18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--Comofaçoparaumadivserexibidaquandoumbotãoestiverdesabilitado,equandoestiverhabilitadoqueroqueessadivfiqueoculta--><divid="conteudo">
  <div id="div1">
    Div Habilitada
  </div>
</div>
<br />
<button id="btn1" disabled>Botão</button>

<br /><br />
<br /><br />

<button id="btnStatus">Mudar Status Botão</button>
    
02.08.2017 / 02:00
0

Just compare if the element has the 'disabled' attribute.

$(document).on('click', '.botao', function () { 
   $this = $(this)
   if($this.attr('disabled') === true)
   { 
      $('div').show(); 
   }else
   {   
      $('div').hide(); 
   }
});

If you want to trigger this in another event, just what the trigger will be:

$(document).ready(function(){

   $this = $('.botao');
   $this.each(function() {
      
 console.log('Disabled:' + $(this).prop('disabled')+' - ' + $(this).text())
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class='div'>div</div>
<button class='botao' disabled>Botão desabilitado X</button>
<button class='botao'>Botão Y</button>
    
01.08.2017 / 16:21