how to get the td id in JavaScript

<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.

Try this,

"<td id='".$s_id."_".$k."' onclick='alert(this.id);'></td>"

How to get <td> id using javascript - Javascript, Hi all, Using javascript how to get the TD id values using the TR id that should work in all browsers. For Example: table id= table1 tr id= row1 td� Using javascript how to get the TD id values using the TR id that should work in all browsers. For Example: <table id="table1>. <tr id="row1">. <td id="r_c_1_1">Row One</td>. <td id="r_c_1_2">Row One</td>. <td id="r_c_1_3">Row One</td>. </tr>.

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>

HTML DOM Table cells Collection, Returns the <td> and/or <th> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range. namedItem(id) � How to get table row data in javascript: When we are working with HTML tables we will get a scenario like to get whole table rows data, or table td values , table cell value in JavaScript. For that we need to read the table by using JavaScript. lets see example on get table cell data using java script.

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.

HTML DOM Table rows Collection, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� if you don't want to assign an id to each TD, look at document.getElementsByTagName ("td") - will return an array of all your TDs as objects, get innerHTML then. dimitar christoff, Oct 8, 2008

JavaScript and jQuery by Examples, Form Input Validation Using JavaScript</h2> <form id="formTest" method="get" action="processData"> <table> <tr> <td><label for="txtName">Name<span� Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

How to Read Data from an HTML Table using JavaScript – Quick Tip, You can assign unique id's to each header (<th>) in the markup and get the index <title>Read Data from HTML Table uisng JavaScript</title> <style> th, td, p,� Get Table Cell Value using Javascript How do I get the text from my cell e..g. Test Text var elTableCells = elTableRow.getElementsByTagName. I'll cover the following topics in the code samples below: Tds.

headers property (td, th) JavaScript, The ids specified by the value of the headers property identify the header elements You can find the related objects in the Supported by objects section below. Definition and Usage. The getElementById() method returns the element that has the ID attribute with the specified value. This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element on your document.

Comments
  • you are sending <td> id in delete_repidfunction. So what's the confusion?
  • you want current id when you click on the td or what you expect?
  • Where do you get confusion in this code? There is not a single line of AJAX or jQuery in your question, that's confusing me
  • Thanku so much for your reply sir.. i got the answer
  • here i put this.id in the onclick like onclick='delete_repid(this.id,$k,$s_id); function still also i am not getting the td sir...
  • then you need to add this id to your anchor tag
  • Is this working at your end? if yes then please accept my answer, Thanks in Advance