0

I need to show the row id of the clicked anchor element in showId():

function showId() {
    var id = $(this).attr('id'); //This return undefined
            
    alert(id);
    return false;
}
<table>
    <thead>
     <tr>
            <th>header1</th> 
            <th>header2</th>    
        </tr>
    </thead>
    <tbody>
        <tr id=1>
            <td><a href="" onclick="return showId()">row1 td1</td> 
            <td><a href="" onclick="return showId()">row1 td2</td>    
        </tr>
        <tr id=2>
            <td><a href="" onclick="return showId()">row2 td1</td> 
            <td><a href="" onclick="return showId()">row2 td2</td>    
        </tr>
        <tr id=3>
            <td><a href="" onclick="return showId()">row3 td1</td> 
            <td><a href="" onclick="return showId()">row3 td2</td>    
        </tr>
    </tbody>
</table>
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39

3 Answers3

2

You need to send clicked element as parameter, so the function have an element to start.

  • Start from: item
  • First parent node: td = item.parentNode
  • Second parent node: tr = item.parentNode.parentNode

function showId(item) {
    var id = item.parentNode.parentNode.id;
            
    alert(id);
    return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
    <tr>
        <th>header1</th> 
        <th>header2</th>    
    </tr>
</thead>
<tbody>
    <tr id="1">
        <td><a href="" onclick="return showId(this)">row1 td1</td> 
        <td><a href="" onclick="return showId(this)">row1 td2</td>    
    </tr>
    <tr id="2">
        <td><a href="" onclick="return showId(this)">row2 td1</td> 
        <td><a href="" onclick="return showId(this)">row2 td2</td>    
    </tr>
    <tr id="3">
        <td><a href="" onclick="return showId(this)">row3 td1</td> 
        <td><a href="" onclick="return showId(this)">row3 td2</td>    
    </tr>
</tbody>
</table>
Triby
  • 1,739
  • 1
  • 16
  • 18
  • 1
    $(item).closest('tr').attr('id'); is better than item.parentNode.parentNode.id; because of item.parentNode.parentNode will break on change of items label. – Bhanu Pratap Apr 28 '20 at 20:55
0

In your code, $(this) refers to your anchor tag. However, the id attribute is defined in the tr tag.

You need to 'move up' the tree to the parent of the parent element. To do this, try

var id = $(this).parent.parent.attr('id');
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
0

Attention, ID attributes should be in "". like id="1"

function showId(sender) {
var id = $(sender).parent().attr('id'); 

alert(id);
return false;
}



<table>
<thead>
    <tr>
        <th>header1</th> 
        <th>header2</th>             
    </tr>
</thead>
<tbody>
    <tr id="1"> //Attention ID in "1"
        <td><a href="" onclick="return showId(this)">row1 td1</td> 
        <td><a href="" onclick="return showId(this)">row1 td2</td>           
    </tr>
    <tr id="2">
        <td><a href="" onclick="return showId(this)">row2 td1</td> 
        <td><a href="" onclick="return showId(this)">row2 td2</td>           
    </tr>
    <tr id="3">
        <td><a href="" onclick="return showId(this)">row3 td1</td> 
        <td><a href="" onclick="return showId(this)">row3 td2</td>           
    </tr>
</tbody>

Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32