anirudh
anirudh

Reputation: 35

how to give unique id to buttons generated dynamically using jquery

<table border="0" class="tableDemo bordered">
    <tr class="ajaxTitle">
        <th width="2%">Sr</th>
        <th >SM</th>
        <th >Campaign</th>
        <th >Day1</th>
        <th >Day2</th>
        <th >Day3</th>
        <th >Day4</th>
        <th >Day5</th>
        <th>Day6</th>
        <th >Day7</th>
        <th>Action</th>
    </tr>
    <?php
    if(count($records)){
     $i = 1;    
     foreach($records as $key=>$eachRecord){
    ?>
    <tr id="<?php echo$eachRecord['id']; ?>">
        <td><?php $i++; ?></td>
        <td class="sm"><?php echo $eachRecord['sm'];?></td>
        <td class="campaign"><?php echo $eachRecord['campaign'];?></td>
        <td class="day1"><?php echo $eachRecord['day1'];?>         
        <button onclick="myFunction()">view</button></td>

        <td class="day2"><?php echo $eachRecord['day2'];?>        
         <button onclick="myFunction()">view</button></td>

        <td class="day3"><?php echo $eachRecord['day3'];?>        
        <button onclick="myFunction()">view</button> </td>

        <td class="day4"><?php echo $eachRecord['day4'];?>        
        <button onclick="myFunction()">view</button></td>

        <td class="day5"><?php echo $eachRecord['day5'];?>       
         <button onclick="myFunction()">view</button> </td>

        <td class="day6"><?php echo $eachRecord['day6'];?>        
        <button onclick="myFunction()">view</button> </td>

        <td class="day7"><?php echo $eachRecord['day7'];?>       
         <button onclick="myFunction()">view</button> </td>
        <td>
            <a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxEdit"><img src="" class="eimage"></a>
            <a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxDelete"><img src="" class="dimage"></a>
        </td>
    </tr>
    <?php }
    }
    ?>
</table> 

This is my table format. I have used ajax and jquery mechanism to dynamically generate new rows where same buttons will be cloned. I want to generate a unique id to each button that is generated. Can anyone please help me on this task?

Upvotes: 1

Views: 1216

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

You can make use of variable i to generate button ids. Put some string for button id and append variable i as shown below

<button onclick="myFunction()" id="firstBtn<?php $i; ?>">view</button></td>

So whole table look like

<table border="0" class="tableDemo bordered">
    <tr class="ajaxTitle">
        <th width="2%">Sr</th>
        <th >SM</th>
        <th >Campaign</th>
        <th >Day1</th>
        <th >Day2</th>
        <th >Day3</th>
        <th >Day4</th>
        <th >Day5</th>
        <th>Day6</th>
        <th >Day7</th>
        <th>Action</th>
    </tr>
    <?php
    if(count($records)){
     $i = 1;    
     foreach($records as $key=>$eachRecord){
    ?>
    <tr id="<?php echo$eachRecord['id']; ?>">
        <td><?php $i++; ?></td>
        <td class="sm"><?php echo $eachRecord['sm'];?></td>
        <td class="campaign"><?php echo $eachRecord['campaign'];?></td>
        <td class="day1"><?php echo $eachRecord['day1'];?>         
        <button onclick="myFunction()" id="firstBtn<?php $i; ?>">view</button></td>

        <td class="day2"><?php echo $eachRecord['day2'];?>        
         <button onclick="myFunction()" id="secondBtn<?php $i; ?>">view</button></td>

        <td class="day3"><?php echo $eachRecord['day3'];?>        
        <button onclick="myFunction()" id="thirdBtn<?php $i; ?>">view</button> </td>

        <td class="day4"><?php echo $eachRecord['day4'];?>        
        <button onclick="myFunction()" id="forthBtn<?php $i; ?>">view</button></td>

        <td class="day5"><?php echo $eachRecord['day5'];?>       
         <button onclick="myFunction()" id="fiftBtn<?php $i; ?>">view</button> </td>

        <td class="day6"><?php echo $eachRecord['day6'];?>        
        <button onclick="myFunction()" id="sixthBtn<?php $i; ?>">view</button> </td>

        <td class="day7"><?php echo $eachRecord['day7'];?>       
         <button onclick="myFunction()" id="seventhBtn<?php $i; ?>">view</button> </td>
        <td>
            <a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxEdit"><img src="" class="eimage"></a>
            <a href="javascript:;" id="<?php echo $eachRecord['id'];?>" class="ajaxDelete"><img src="" class="dimage"></a>
        </td>
    </tr>
    <?php }
    }
    ?>
</table>

Upvotes: 1

Related Questions