Hongbo Miao February 2016

How to retrieve data from a table cell in angular 2

I am using Angular 2. I try to retrieve data from a table cell. Is there an Angular 2 way to do this?

I tried to do like this, but I got undefined value.

<table>
    <thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td (click)="getValue($event)">John</td>
        <td>Doe</td>
        <td>john@example.com</td>
    </tr>
    <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
    </tr>
    </tbody>
</table>

getValue(event:any) {
    let value = event.target.value;
    console.log("value", value);
}

Answers


Günter Zöchbauer February 2016

use instead

let value = event.target.text;

or

let value = event.target.innerHTML;  

The <td> doesn't have a value property.

Post Status

Asked in February 2016
Viewed 3,799 times
Voted 7
Answered 1 times

Search




Leave an answer