Mir
Mir

Reputation: 1

Enable/Disable input when checkbox is checked

So I have a checkbox and an "input number" and I want to enable/disable that input when check/uncheck the checkbox with jquery!!

Here is the code:

 $(document).ready(function() { 
      $("#criar").click(function() {
        $('input[name^="quant"]').each(function() {
          $(this).attr('disabled', 'disabled');
  });
  $('#myform').change(function(){
        var i=0;
        $('input[name^="tipo"]').each(function() {
          if($(this).prop('checked')){
            $('#textnumber').eq(i).prop('disabled', false);
          }else{                                         
            $('#textnumber').eq(i).prop('disabled',true);
          }
           i++;
      });
    });
  });
}); 

And the html code:

<input type="checkbox" name="tipo[]" id="tipo">
<input type="number" name="quant[]" min="1" max="50" id="textnumber[]">

This is working but only with the first checkbox and input as you can see here

Upvotes: 0

Views: 179

Answers (1)

Deepak A
Deepak A

Reputation: 1636

Use ".is(':checked')" to check if select box is selected .Hope this helps,I think this is what you were looking for

$(document).ready(function() { 
  $('.number').prop('disabled', true);
  $('#tipo').click(function(){
  if($(this).is(':checked'))
  {
        $('.number').prop('disabled', false);
  }
  else
        $('.number').prop('disabled', true);
      })

 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="tipo[]" id="tipo">
<input type="number" name="quant[]" min="1" max="50" id="textnumber[]" class="number">

Upvotes: 1

Related Questions