0

I have a list of data getting from database, and i want to enable input field on checked checkbox. However its working only with the first line. Here is HTML code:

<?php
    require_once('inc/config.php');
    include 'verification/verify_form_details.php';
?>
<html>
<head>
  <title>getElementById example</title>
  <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
</head>
<body>
    <?php 
        $result2 = getProcessID();
    ?>
    <label>Process: <small style="color:red;">*</small>             
    <?php 
        while ($row = mysql_fetch_array($result2))
        {
            $row[0] = cleanOutputData($row[0]);
    ?>      
    <div>       
        <label>
        <input type="checkbox" class="checkbox" name="process[]" id=<?php echo $row[0] ?>  value=<?php echo $row[0]?> /><?php echo $row[0] ?>
        </label>                                        

        <label>
        <input type="text" class="field" disabled name="number[]" id=<?php echo $row[0] ?> />
        </label>

    </div>
    <?php
        }
        mysql_free_result($result2);
    ?>
    </label>
</body>
</html>

And javascript function:

<script>
    $(document).ready(function () {
            $('.checkbox').change(function() {
                $('.field').attr('disabled',!this.checked)
            });
    });
</script>

Any ideas how i can do it? Thank you

5
  • Because same ID use class instead. ID should be unique Commented Sep 6, 2016 at 2:43
  • @guradio I tried class and now after pressing on one checkbox its enabling all fields. edited my question also Commented Sep 6, 2016 at 3:00
  • use $(this).parent().next().find('.field').attr('disabled',!this.checked) Commented Sep 6, 2016 at 3:03
  • @guradio now all the fields are staying disabled even when checkbox is checked Commented Sep 6, 2016 at 3:10
  • please see answer below Commented Sep 6, 2016 at 3:15

1 Answer 1

4

$('.checkbox').change(function() {
  $(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>


<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>


<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>

  1. Use class instead of ID
  2. Use this context to refer to changed element
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.