skip to Main Content
<table class="table" style="margin-bottom:0px!important"> 
    <b>
    <? if(!empty($channelBase['rep_ids']))
    {
        $s_id=$channelBase['id'];
        $i=0;   
        $temp='';

        $reps_channl=explode(",",$channelBase['rep_ids']);
        foreach($reps_channl as $k)
        {
            $added_reps = $rep_names[$k];
            if($i==0){
            $temp.='<tr style="border-top:none;">';     
            }
            $temp.="<td id='".$s_id."_".$k."' class='repclicked' style='border-top:none;'>$added_reps <a href='#' class='btn btn-xs btn-icon btn-circle ' onclick='delete_repid($k,$s_id);'><i class='fa fa-close'></i></a>
            </td>";
            $i++;
            if($i==5)
            {
            $temp.='</tr>';
            $i=0;
            }
        }
        echo $temp;
    }
    ?>
    </b>
</table>

Here I have a td id with a number value. here I am getting confusion on how to get the td id in javascript. can anyone please help me.

3

Answers


  1. Try this,

    "<td id='".$s_id."_".$k."' onclick='alert(this.id);'></td>"
    
    Login or Signup to reply.
  2. Here is how you can get Ids of td inside table

    for (let row of mytab1.rows) 
    {
        for(let cell of row.cells) 
        {
           console.log(cell.id)
        }
    }
    <div id="myTabDiv">
    <table name="mytab" id="mytab1">
      <tr> 
        <td id="id_1">col1 Val1</td>
        <td id="id_2">col2 Val2</td>
      </tr>
      <tr>
        <td id="id_3">col1 Val3</td>
        <td id="id_4">col2 Val4</td>
      </tr>
    </table>
    </div>
    Login or Signup to reply.
  3. You are trying to mix PHP and javascript, which you can not do. Everything in PHP is separate from javascript and the two do not have access to identifiers from each other.

    The reason is this:

    PHP code is completely run BEFORE the page is loaded. Once the page is loaded completely in PHP, it is then sent to the browser where the javascript acts upon whatever the result of the PHP code was.

    Try this:

    on a page enter:

     <pre>
     <?php
      $a = "1"; 
      $b = "2";  
     print_r($a); 
     print_r($b); 
     ?>
     </pre>
    

    Now load the page and right click on the page and go to “inspect ” or “view source”

    you will see that the source of the page has no php code and is only

     <pre>12</pre>
    

    This is because all PHP code is processed BEFORE the page is loaded, whereas javascript is processed AFTER the page is sent to the browser.

    You have to do everything involving PHP Ids first and completely separate from javascript, and likewise javascript must be completely separate from PHP.

    For your case you must make a separate request in order to modify an array in PHP and then either reload the page or use AJAX to load the updated data.

    The type of action you are trying to accomplish is impossible the way you are trying to do it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search