1

I have checkboxes generated dynamically through php and i would like to hide a certain button if all the checkboxes having a similar class value are not checked and reenable it if they are checked:

The php code that generates the checkboxes is

<table class="table table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Description</th>
                    <th>Status</th>
                    <th>Comment</th>
                </tr>
                </thead>
                <tbody>

 foreach ($checks as $m => $check) {
    $item ="";
    $checkbox ="";
   $textinput ="";
   $displayx="";

if ($check->mandatory_customer == 1) { //mandatory customer checks
 $displayx .="<i style='color:red;'>*</i>";
  $item .=  $check->item.$displayx;
   $checkbox .='<input type="checkbox" class="1" id="'.$m.'"' ;
   $textinput .='<input type="text" class="1" id="'.$m.'"' ;

     } else { //not mandatory customer
    $item .=  $check->item;
   $checkbox .='<input type="checkbox" class="0" id="'.$m.'"' ;
 $textinput .='<input type="text" class="0" id="'.$m.'"' ;

    }

echo "<tr id='" . $m . "'>";
echo "<td>" . $m . "</td>";
echo "<td>" . $item . "</td>";
echo "<td>".$checkbox."</td>";
echo "<td>".$textinput."</td>";
echo "</tr>";
}
  ?>
     }

</tbody>

I also have a button that i would like to disable if all checkboxes with class 1 are not checked and enable it if they are all checked using jquery

            <button class="btn btn-success" id="approve_btn">Approve</button>

I have tried:

$('tbody tr').each(function () {
    if ($(this).attr('class:1').find('.myCheckBox').prop('checked')) {
        doEnableButton = true;
    }
    if (!doEnableButton) {
        $('#approve_btn').prop('disabled', 'disabled')
    }
    else {
        $('#approve_btn').removeAttr("disabled");
    }
});

});

I have checked on several resources but most of them are not helpful The above fails how do i go about this

1 Answer 1

1

Try this

    $(document).ready(function () {
        $(":checkbox.1").on("change", function () {
            if ($(":checkbox.1").not(":checked").length > 0)
                $("#approve_btn").prop("disabled", "disabled");
            else
                $("#approve_btn").removeAttr("disabled");
        });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Please could you also check this one: stackoverflow.com/questions/40003919/…

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.