Sachin Vairagi Sachin Vairagi - 2 months ago 6x
Javascript Question

How to get which td clicked in JavaScript?

I've got the following table:

<tr data-uom_id="1" id="id-742">
<td style="width: 15%; vertical-align: middle; display: none;"><img src="left-arrow.png>"</td>
<td style="width: 70%; text-align: left;" class="center">some text here</td>
<td style="width: 15%; vertical-align: middle;"><img src="right-arrow.png></td>

Here my
element has 3
. Can I get which
is clicked? Please check attached image for more clarification.

enter image description here


You can try the following code:

Edit - changed the code as per requirement

function whichOne() {
    document.getElementById("center").style.textAlign =;

document.getElementById('left').onclick = whichOne;
document.getElementById('right').onclick = whichOne;
document.getElementById('center').onclick = whichOne;
<table border="1">
     <td style="width: 15%;" id="left">1</td>
     <td style="width: 70%; text-align: center;" id="center">2</td>
     <td style="width: 15%;" id="right">3</td>