2

I have here multiple checkboxes which data is from the database. I want that the textbox will be remained disable until I checked or clicked the checkbox. lets just say this is the data that came from the database for example.

$(function() {
  $('.check').click(function() {
    if ($(this).is(':checked')) {
      $('.checks').removeAttr('disabled');
      $('.checks').focus();
    } else {
      $('.checks').attr('disabled', 'disabled');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check"> Aldrin
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Dafne
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Diane
<input type="text" class="checks" disabled>

2
  • read about on('change') or use ajax to check if X checkbox was checked and then enable other checkbox to be checked or not Commented Oct 5, 2021 at 17:24
  • 1
    I will thanks for the offer it will help me to enhance my skills Commented Oct 5, 2021 at 17:28

2 Answers 2

2

Sure, you can reduce what you have to just the following, using $(this) to refer to the specific checkbox you're checking/unchecking:

$(function() {
  $('.check').click(function() {
    $(this).next().prop('disabled', !$(this).is(':checked'))
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check"> Aldrin
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Dafne
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Diane
<input type="text" class="checks" disabled>

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

1 Comment

Thank you sir...for this code.it help me to solve my problem
0

$(function() {
  $('.check').click(function() {
    if ($(this).is(':checked')) {
      $(this).next('.checks').removeAttr('disabled');
      $(this).next('.checks').focus();
    } else {
      $(this).next('.checks').attr('disabled', 'disabled');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check"> Aldrin
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Dafne
<input type="text" class="checks" disabled>

<input type="checkbox" class="check"> Diane
<input type="text" class="checks" disabled>

1 Comment

Thanks you sir for this answer

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.