PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] how to uncheckbox in data table when button in table click

 September 06, 2022     ajax, html, javascript, jquery     No comments   

Issue

I have code like this

<table id='data-table'>
 <thead>
   <tr>
     <th>No</th>
     <th>Name Items</th>
     <th>Qty</th>
     <th>Price</th>
     <th>Action</th>
     <th>Check</th>
   </tr>
 </thead>
 <tbody>
   <php foreach($data as $d) : ?>
     <tr>
     <td><?= $no++ ?></td>
     <td><?= $d['name'] ?></td>
     <td><?= $d['qty'] ?></td>
     <td><?= $d['price'] ?></td>
     <td><button type='button' class='btn' name='up'>up</button></td>
     <td><input type='checkbox'></td>
     </tr>
   <php endforeach ?>
 </tbody>

</table>

I want when click button, that qty update data and checkbox if that check then uncheck, I'm use jquery like this but i don't know how can i uncheckbox when button click. Thank you in advance

 $('#data-table tbody').on('click', '.btn[name="up"]', function() {
    const row = $(this).closest('tr')[0];
    const qty = Number(row.cells[2].innerHTML);
    const tqty = qty + 1;
    row.cells[2].innerHTML = tqty;
  });

Solution

You should use jQuery all the way or not at all

Also you are missing <tr></tr>

$('#data-table tbody').on('click', '.btn[name="up"]', function() {
  const $row = $(this).closest('tr');
  const $cells = $row.find("td");
  const $qtyCell = $cells.eq(2);
  const qty = +$qtyCell.text()
  $qtyCell.text(qty + 1);
  $cells.eq(5).find("[type=checkbox]").prop("checked",false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table id='data-table'>
  <thead>
    <tr>
      <th>No</th>
      <th>Name Items</th>
      <th>Qty</th>
      <th>Price</th>
      <th>Action</th>
      <th>Check</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Name 1</td>
      <td>5</td>
      <td>2.00</td>
      <td><button type='button' class='btn' name='up'>up</button></td>
      <td><input type='checkbox'></td>
    </tr>
    <tr>
      <td>2</td>
      <td>Name 2</td>
      <td>8</td>
      <td>4.00</td>
      <td><button type='button' class='btn' name='up'>up</button></td>
      <td><input type='checkbox'></td>
    </tr>
  </tbody>
</table>



Answered By - mplungjan
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing