简体   繁体   中英

How to create dynamic id to a single button in foreach loop using html & js

I'v to generate different Id's to a Single button which is under for loop. I'd created table tbl_tableMaster (id , tblNo )

I'd created a view which displays the tblno on a button by using foreach loop but, what I've to do is:

  1. generate different id's to a button
  2. onClick of any button(btnclick) the value should be display on a label(lblTableNo)

<?php foreach($tables as $row){ ?>
    <button type = "Submit"
      onclick = "changeLabel()"
      id = "btnclick"
      class = "btn btn-primary mt-1"
      value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
    <input type = "text"
      name = "txtID"
      id = "txtID<?php echo $row->table_id;?>"
      value = "<?php echo $row->table_id;?>"
      hidden >
<?php } ?>

<div class="card-header ">
  <strong>Table No :</strong> <label id="lblTableNo"></label>
</div>

Try this code it works...

<?php $no=1;  foreach($tables as $row){ ?>
        <button type = "Submit"
          onclick = "changeLabel(<?php echo $no; ?>)"
          id = "btnclick<?php echo $no; ?>"
          class = "btn btn-primary mt-1"
          value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
        <input type = "text"
          name = "txtID"
          id = "txtID<?php echo $row->table_id;?>"
          value = "<?php echo $row->table_id;?>"
          hidden >
    <?php $no++; } ?>

    <script type="text/javascript">


        function changeLabel(id) {

         var loanamount = document.getElementById('btnclick' + id).value;

          document.getElementById('lblTableNo').innerHTML = loanamount;

        };
        </script>

    <div class="card-header ">
      <strong>Table No :</strong> <label id="lblTableNo"></label>
    </div>

You could edit the button onclick to directly change the label, like this :

<?php foreach($tables as $row){ ?>
    <button type = "Submit"
      onclick = "document.getElementById('lblTableNo').innerHTML = this.value;"
      id = "btnclick"
      class = "btn btn-primary mt-1"
      value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
    <input type = "text"
      name = "txtID"
      id = "txtID<?php echo $row->table_id;?>"
      value = "<?php echo $row->table_id;?>"
      hidden >
<?php } ?>

you can do this like <tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td> <tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td> on one of your table data tag. then on your javascript query

<script type="text/javascript">
        $(document).ready(function(){
            $(".approve").click(function(){
                var $row = $(this).closest('tr'); //for the row
                var $transactionCode = $row.find(".transactionCodeData").text();

                //try if the js script works
                alert($transactionCode);

                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>controller/method",
                    data: { transactionKey: $transactionCode },
                    success: function(data){
                        location.reload(); 
                    }
                });
            });
        });
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM