kalpita
kalpita

Reputation: 53

Get an id on checkbox

I have html code like this.

HTML

<div class="container" style="padding-top: 30px; padding-bottom: 30px; width: 1089px;">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">

            <div class="fresh-table toolbar-color-orange">
            <!--    Available colors for the full background: full-color-blue, full-color-azure, full-color-green, full-color-red, full-color-orange                  
                    Available colors only for the toolbar: toolbar-color-blue, toolbar-color-azure, toolbar-color-green, toolbar-color-red, toolbar-color-orange
            -->

                <div class="toolbar">

                  <button id="selectAll" class="btn btn-default" style="padding-right:70px;">Select All</button>

                    <button id="popup" onclick="div_show()" class="btn btn-default" style="margin-left: 650px;">Send</button>
                </div>

                <table id="fresh-table1" class="table1">
                    <thead>
                       <tr>   
                       <th></th>  
                          <th data-field="id" data-sortable="true">ID</th>
                        <th data-field="name" data-sortable="true">First Name</th>
                        <th data-field="salary" data-sortable="true">Last Name</th>
                        <th data-field="country" data-sortable="true">Date Of Birth</th>
                        <th data-field="city">Gender</th>
                        <!-- <th data-field="actions" data-formatter="operateFormatter1" data-events="operateEvents1">Actions</th> -->
   </tr>           
                    </thead>

                    <tbody>
                            <tr>
                          <?php  
                          foreach ($user->result_array () as $row ) {?>

                          <td><input type="checkbox"></td>
                              <td><?php  echo $row['user_id'];?></td>
                                <td><?php  echo $row['firstname'];?></td>
                                <td><?php  echo $row['lastname'];?></td>
                                <td><?php  echo $row['dob'];?></td>
                                 <td><?php if($row['gender']==1){ echo                        'Male';}else{echo 'Female';}?></td> 

                            </tr>           
   <?php }?>                          
                    </tbody>
                </table>
            </div>             
        </div>
 </div>
    </div>

And my javascript just like this.

Javascript

  <script>

    $(document).ready(function () {
    $('body').on('click', '#selectAll', function () {

    if ($(this).hasClass('allChecked')) {
        $('input[type="checkbox"]', '#fresh-table1').prop('checked', false);
    } else {
        $('input[type="checkbox"]', '#fresh-table1').prop('checked', true);
    }
    $(this).toggleClass('allChecked');
  })
});

I am using Codeigniter Framework. I used javascript in my table form. I want to get user_id on the select button so that I can send the notifications to the selected id(s). I want user_id in an array format.

Upvotes: 0

Views: 47

Answers (1)

Milan Chheda
Milan Chheda

Reputation: 8249

:checkbox:checked selector and map to create an array of the user_ids:

Below is the working demo:

function getAllCheckedUserIds() {
  var userIds = $('input:checkbox:checked').map(function() {
    return $(this).parent().next().text();
  }).get();
  var userIds = userIds.filter(function(v) {
    return v !== ''
  });
  console.log(userIds);
  return userIds;
}

$(document).ready(function() {
  $('body').on('click', '#selectAll', function() {
    if ($(this).is(":checked")) {
      $('input[type="checkbox"]', '#fresh-table1').prop('checked', true);
      getAllCheckedUserIds();
    } else {
      $('input[type="checkbox"]', '#fresh-table1').prop('checked', false);
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="selectAll" type='checkbox' class="allChecked" /> select All

<table id='fresh-table1'>
  <tr>
    <td><input type='checkbox' /></td>
    <td>User id: 1</td>
  </tr>
  <tr>
    <td><input type='checkbox' /></td>
    <td>User id: 2</td>
  </tr>
</table>

Upvotes: 1

Related Questions