1
                        <tbody>
                            <?php 
                                $result = $connect->query("SELECT * FROM fees;") or die($connect->error());
                                while($row = $result->fetch_assoc()){ 
                            ?>
                            <tr>
                                <td>              
                                    <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="check_amount">
                                    <label class="custom-control-label" for="check_amount"><?php echo $row['id']; ?></label>
                                </div>
                                </td>
                                <td><?php echo $row['fee_name']; ?></td>
                                <td name="amount"><?php echo $row['amount']; ?></td>
                                <td><?php echo $row['type']; ?></td>
                            </tr>
                            <?php } ?>
                        </tbody>

This is my table inside my modal, I want todo after the checkbox being checked it will automatically calculate in my input named "tp".

table rows a cell named "amount" which will be the value of checkbox

`

<div class="form-group">
  <label for="tp">Total payment</label>
  <input type="number" class="form-control" id="tp" name="tp" required disabled>
</div>

`


Currently this is the only code that I have in my js


        $(function(){ 
    $(".check_amount").click(function(event) {
        var total = 0;
        $(".check_amount:checked").each(function() {
        total += parseInt($(this).val());
        });
            $('#tp').val(total);
        });
    });
    $(document).ready(function () {
    var total2=0;
        $(".check_amount").each(function() {
    total2 += parseInt($(this).val());

    });
        $('#tp').val(total2);
    });

Lastly I want to achieve dynamically when you uncheck the checkbox at the same time the total value will also deducted.

Any tips for this? your help is highly appreciated!!

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Since this doesn't depend on PHP, please create a snippet with a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your issue. – El_Vanja Apr 27 '21 at 08:48
  • https://jsfiddle.net/katalepsy/0m8wpo6t/12/#&togetherjs=tGQAPKdJce currently this is what i got right now – Janbres Gagaracruz Apr 27 '21 at 08:59

1 Answers1

2

You cannot use same ids for mutliple elements so instead use class i.e : check_amount. Then , whenever user click on checkbox simply loop through checked checkboxes and get name="amount" values using .closest('tr').find('td[name=amount]') and finally add result inside your input.

Demo Code:

$(function() {
  $(".check_amount").click(function(event) {
    var total = 0;
    //loop through checked checkbox
    $("tbody input[type=checkbox]:checked").each(function() {
      total += parseInt($(this).closest('tr').find('td[name=amount]').text().trim()); //get td value
    });
    $('#tp').val(total);
  });
  // $(".check_amount").trigger('click')//on load
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>

    <tr>
      <td>
        <div class="custom-control custom-checkbox">
        <!--added class here-->
          <input type="checkbox" class="custom-control-input check_amount">
          <label class="custom-control-label" for="check_amount">1</label>
        </div>
      </td>
      <td>
        A1
      </td>
      <td name="amount">
        123
      </td>
      <td>
        A
      </td>
    </tr>
    <tr>
      <td>
        <div class="custom-control custom-checkbox">
          <input type="checkbox" class="custom-control-input check_amount">
          <label class="custom-control-label" for="check_amount">1</label>
        </div>
      </td>
      <td>
        A13
      </td>
      <td name="amount">
        12
      </td>
      <td>
        A3
      </td>
    </tr>

  </tbody>
</table>

<div class="form-group">
  <label for="tp">Total payment</label>
  <input type="number" class="form-control" id="tp" name="tp" required disabled>
</div>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Nice it works just like what I wanted! thank you for your help! lastly, can I ask why you didn't use id to call multiple elements instead of that you use class? I just want to enlighten myself for getting confused. – Janbres Gagaracruz Apr 27 '21 at 09:06
  • 1
    Hi , ids should be unique and you cannot use **same** ids for mutliple elements . But , same class can be use by mutliple elements so here you need to trigger all checkboxes so for that use class as selector . Also , check [this](https://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme) post will help you to understand more. – Swati Apr 27 '21 at 09:13
  • I got it already thank you for enlightening me about that. – Janbres Gagaracruz Apr 27 '21 at 09:24
  • Hi i'd like to ask question again,, `name += ($(this).closest('tr').find('td[name=selected_fees]').val()+" ");` I also want to get selected fee name `` but it keeps on saying in my console that cannot be parse and out of range, can you suggest a solution here. tyy – Janbres Gagaracruz Apr 27 '21 at 10:36
  • 1
    Hi, this `val()` should be `text()` .Also , they are strings(alphabets) so remove `parseInt` – Swati Apr 27 '21 at 11:48
  • I already remove `parseInt` ang replaced `val()` to `text()` but it still cannot be parse and out of range. Should I put it within the function or do i need to create another function for that? https://jsfiddle.net/katalepsy/v6x0wcfn/41/#&togetherjs=VgA8Qvj13N – Janbres Gagaracruz Apr 27 '21 at 12:17
  • 1
    see updated [code](https://jsfiddle.net/e3gkd6h9/) . Also , `type="text"` for name input. – Swati Apr 27 '21 at 12:27
  • 1
    I'm so glad for your patience towards me thank you so much. I would love if I can get your Discord or what ever platform you are using ^^ – Janbres Gagaracruz Apr 27 '21 at 12:33