0

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

1
  • could you post the rest of the html please. I'm not sure what you're clicking on. what is $('#criar'). also depending on what you want to do you could create a class for all the checkboxes and then use each to loop over them and use $(this) to capture checkbox you're actually clicking on Commented Apr 5, 2019 at 13:10

1 Answer 1

1

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">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.