Sizzling Code
Sizzling Code

Reputation: 6070

jquery show button when checkbox is checked

I want to show the delete button below the table if any of the row checkboxes are checked. Otherwise, the button should stay hidden.

Table I want it to be implemented. screenshot of table: enter image description here

This is the html table code behind.

      <table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
        <thead>
          <tr>
            <th style="width:0px; padding-right:0px;" class="jv no_sort"> <label class="checkbox ">
                <input type="checkbox">
              </label>
            </th>
            <th style="width:200px;" class=""> Skill </th>
            <th style="width:300px;" class="to_hide_phone  no_sort"> Description </th>
            <th class=""> Rating </th>
            <th style="width:200px;" class="ue no_sort"> Date </th>
            <th class="ms no_sort "> Actions </th>
          </tr>
        </thead>
        <tbody>
        <?php echo $skills; ?>
        </tbody>
      </table>

This is the skills variable:

$skills="";
while($row=mysql_fetch_array($skills_list))
{
    $showskillid=$row['skill_id'];
    $showskillname=$row['skill_name'];
    $showskilldescription=$row['skill_desc'];
    $showskillrating=$row['skill_rating'];
    $showskill_lastupdated=$row['last_updated'];
    $skills .="<tr>
                    <td><label class=\"checkbox\">
                        <input type=\"checkbox\" class=\"chkboxes\" name=\"ids[]\">
                      </label></td>
                    <td class=\"to_hide_phone\"> $showskillname </td>
                    <td class=\"to_hide_phone\">$showskilldescription</td>
                        <td> $showskillrating </td>
                    <td> $showskill_lastupdated </td>
                    <td class=\"ms\"><div class=\"btn-group1\"> <a href=\"skill_edit.php?id=$showskillid\" class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"left\" data-original-title=\" edit \"><i class=\"gicon-edit\"></i></a> <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\"><i class=\"gicon-eye-open\"></i></a> <a class=\"btn  btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a> </div></td>
                  </tr>";
}

The Jquery which I tried but I know I am newbie in jquery so need help to fix it.

    <script type="text/javascript">
$(document).ready(function() {
    $('.delbtn').hide();
 });


$('.chkboxes').click(function(){
    $('.delbtn').toggle();
});

</script>

Jquery I tried it somehow works like when I click one checkbox the button shows up and when I go and click the other checkbox the button disappears?

I know toggle should not be used here.. What instead should I use here to fix this issue, or if there is more better option to fix it I am open to it..

Upvotes: 3

Views: 4527

Answers (1)

Barmar
Barmar

Reputation: 780984

This will show the Delete button when at least one checkbox is checked.

$('.chkboxes').change(function(){
    $('.delbtn').toggle($('.chkboxes:checked').length > 0);
});

But instead of hiding it, the more common approach is to enable and disable the button:

$('.delbtn').prop('disabled', true);

$('.chkboxes').change(function(){
    $('.delbtn').prop('disabled', $('.chkboxes:checked').length == 0);
});

Upvotes: 3

Related Questions